Skip to main content

http

Jump to function (20)

http/emit-response#

(emit-response response)

Emits the response by sending headers and outputting the body.

Example:

(emit-response (response-from-string "Hello World")) ; => nil

http/files-from-globals#

(files-from-globals & [files])

Extracts uploaded files from $_FILES and normalizes them to uploaded-file structs. Handles both flat and nested file specs from multipart uploads. Returns a map of field name to uploaded-file(s).

Example:

(files-from-globals) ; => {:avatar (uploaded-file "/tmp/phpYzdqkD" 1024 0 "photo.jpg" "image/jpeg")}

http/headers-from-server#

(headers-from-server & [server])

Extracts all HTTP headers from the $_SERVER variable. Strips the HTTP_ prefix, normalizes underscores to hyphens, handles CONTENT_* keys, and resolves REDIRECT_* keys. Returns a map with keyword keys.

Example:

(headers-from-server) ; => {:host "example.com" :content-type "application/json"}

http/html-response#

(html-response status body)

Creates a response with status and an HTML body and a Content-Type: text/html; charset=utf-8 header.

Example:

(html-response 200 "<h1>Hi</h1>") ; => (response 200 {:content-type "text/html; charset=utf-8"} "<h1>Hi</h1>" "1.1" "OK")

http/json-response#

(json-response status data)

Creates a response with status and a JSON-encoded data body and a Content-Type: application/json header.

Example:

(json-response 200 {:message "pong"}) ; => (response 200 {:content-type "application/json"} "{\"message\":\"pong\"}" "1.1" "OK")

http/request#

(request method uri headers parsed-body query-params cookie-params server-params uploaded-files version attributes)

Creates a new request struct.

http/request-from-globals#

(request-from-globals)

Extracts a request from $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES and, for JSON requests, the raw request body (php://input). Requires a web request context (PHP-FPM, mod_php, or php -S); it cannot be used from the REPL or a CLI script - use request-from-map for testing.

Example:

(request-from-globals) ; => (request "GET" (uri ...) {...} nil {...} {...} {...} {...} "1.1" {})

http/request-from-globals-args#

(request-from-globals-args server get-parameter post-parameter cookies files & [raw-body])

Extracts a request from args. The optional raw-body (the raw request body string) is decoded into :parsed-body when the Content-Type is application/json; form bodies still come from post-parameter.

Example:

(request-from-globals-args php/$_SERVER php/$_GET php/$_POST php/$_COOKIE php/$_FILES (php/file_get_contents "php://input")) ; => (request "GET" (uri ...) {...} nil {...} {...} {...} {...} "1.1" {})

http/request-from-map#

(request-from-map {:method method, :version version, :uri uri, :headers headers, :parsed-body parsed-body, :query-params query-params, :cookie-params cookie-params, :server-params server-params, :uploaded-files uploaded-files, :attributes attributes})

Creates a request struct from a map. Supports the optional keys :method, :uri (a string parsed via uri-from-string, or a uri struct used as-is), :headers, :parsed-body, :query-params, :cookie-params, :server-params, :uploaded-files, :version, and :attributes. Missing keys default to nil, {}, [], or "1.1" as appropriate.

Example:

(request-from-map {:method "POST" :uri "https://api.example.com/users"}) ; => (request "POST" (uri ...) {} nil {} {} {} [] "1.1" {})

http/request?#

(request? x)

Checks if x is an instance of the request struct.

http/response#

(response status headers body version reason)

Creates a new response struct.

http/response-from-map#

(response-from-map {:status status, :headers headers, :body body, :version version, :reason reason})

Creates a response struct from a map. Optional keys: :status (defaults 200), :headers ({}), :body (""), :version ("1.1"), and :reason (auto-filled from the HTTP status code when omitted).

Example:

(response-from-map {:status 200 :body "Hello World"}) ; => (response 200 {} "Hello World" "1.1" "OK")

http/response-from-string#

(response-from-string s)

Creates a 200 OK response with the given body string. Shorthand for (response-from-map {:body s}).

Example:

(response-from-string "Hello World") ; => (response 200 {} "Hello World" "1.1" "OK")

http/response?#

(response? x)

Checks if x is an instance of the response struct.

http/uploaded-file#

(uploaded-file tmp-file size error-status client-filename client-media-type)

Creates a new uploaded-file struct.

http/uploaded-file?#

(uploaded-file? x)

Checks if x is an instance of the uploaded-file struct.

http/uri#

(uri scheme userinfo host port path query fragment)

Creates a new uri struct.

http/uri-from-globals#

(uri-from-globals & [server])

Extracts the URI from the $_SERVER variable. The scheme is read from HTTP_X_FORWARDED_PROTO, REQUEST_SCHEME, or HTTPS; the path from REQUEST_URI; and the host/port via the $_SERVER host fields. Returns a uri struct.

Example:

(uri-from-globals) ; => (uri "https" nil "example.com" 443 "/path" "foo=bar" nil)

http/uri-from-string#

(uri-from-string url)

Parses a URI string into a uri struct. Handles UTF-8 URLs and IPv6 addresses in brackets. Throws InvalidArgumentException on a malformed string.

Example:

(uri-from-string "https://example.com/path?foo=bar") ; => (uri "https" nil "example.com" nil "/path" "foo=bar" nil)

http/uri?#

(uri? x)

Checks if x is an instance of the uri struct.