// retoor <retoor@molodetz.nl>
import "web" for Request
var headers = {
"Content-Type": "application/json",
"Accept": "text/html",
"Cookie": "session=abc123; user=alice"
}
var req = Request.new_("POST", "/api/users", {"page": "1", "limit": "10"}, headers, "{\"name\":\"Alice\"}", {"id": "42"}, null)
System.print(req.method) // expect: POST
System.print(req.path) // expect: /api/users
System.print(req.query["page"]) // expect: 1
System.print(req.query["limit"]) // expect: 10
System.print(req.params["id"]) // expect: 42
System.print(req.body) // expect: {"name":"Alice"}
System.print(req.header("Content-Type")) // expect: application/json
System.print(req.header("content-type")) // expect: application/json
System.print(req.header("ACCEPT")) // expect: text/html
System.print(req.header("X-Missing") == null) // expect: true
var json = req.json
System.print(json["name"]) // expect: Alice
var cookies = req.cookies
System.print(cookies["session"]) // expect: abc123
System.print(cookies["user"]) // expect: alice
var formBody = "username=bob&password=secret"
var formReq = Request.new_("POST", "/login", {}, {"Content-Type": "application/x-www-form-urlencoded"}, formBody, {}, null)
var form = formReq.form
System.print(form["username"]) // expect: bob
System.print(form["password"]) // expect: secret