Language

Incoming HTTP requests

A script that always generates the same output isn't very interesting. Let's use request parameters instead. The following script sends greetings to anyone mentioned in the whom parameter. If it's missing, greet the whole world.

package main

import (
    "fmt"
    "net/http"
)

func Hook(r *http.Request) {
    whom := r.FormValue("whom")
    if whom == "" {
        whom = "world"
    }
    fmt.Printf("Hello, %s!\n", whom)
}
use Hookscript;

my $whom = $req->param('whom') // 'world';
say "Hello, $whom!";
:- use_module(library(hookscript)).

hook :-
    req:param(whom,world,Whom),
    format("Hello, ~s!~n", [Whom]).
from hookscript import request

whom = request.values.get('whom','world')
print('Hello, %s!' % whom)

Each language implementation strives to represent an HTTP request idiomatically. See the language specific documentation for details on how it's done in Go Perl Prolog Python .

Next