Standard library
HTTP server
HTTP serve loop and connection handling.
Introduction
HTTP serve loop and connection handling. Import std/net/http to bind the module as http. Call free functions as http.name(...). Struct methods use a receiver value.
/ std/net/http
This page is the package reference for std/net/http. Private helpers used only by co-located tests are not listed.
Struct · request
HTTP request shape (re-export). The shape is exported as http.request. Methods on the receiver are listed next.
/ std/net/http
; shape: http.request
$ req = http.parse_request(data)
Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
request · is_get
Reports whether the request method is GET. Same shape as std/net/request. Call form: request.is_get().
/ std/net/http
$ ok = req.is_get()
Parameters: No parameters. Returns: Boolean.
request · is_post
Reports whether the request method is POST. Same shape as std/net/request. Call form: request.is_post().
/ std/net/http
$ ok = req.is_post()
Parameters: No parameters. Returns: Boolean.
request · has_body
Reports whether the request body string is non-empty. Same shape as std/net/request. Call form: request.has_body().
/ std/net/http
$ ok = req.has_body()
Parameters: No parameters. Returns: Boolean.
Struct · response
HTTP response shape (re-export). The shape is exported as http.response. Methods on the receiver are listed next.
/ std/net/http
; shape: http.response
$ res = http.text_response(200, "ok")
Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
response · set_header
Sets a response header and returns the response for chaining. Same shape as std/net/response. Call form: response.set_header(name, value).
/ std/net/http
$ res = http.text_response(200, "ok")
res.set_header("X-Demo", "1")Parameters: name: header name. value: header value. Returns: The response receiver.
Struct · server
HTTP server shape (re-export). The shape is exported as http.server. Construct or obtain values through the free functions below when the package provides them.
/ std/net/http
; shape: http.server
; use http.serve for a running server
Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
Functions
Free functions on http. Each function has a short description, an example, then parameters and return shape.
text_response
Builds a plain-text HTTP response product. Call form: http.text_response(status, body).
/ std/net/http
$ res = http.text_response(200, "ok")
Parameters: status: HTTP status code. body: response body text. Returns: HTTP response product.
html_response
Builds an HTML HTTP response product. Call form: http.html_response(status, body).
/ std/net/http
$ res = http.html_response(200, "<p>ok</p>")
Parameters: status: HTTP status code. body: HTML text. Returns: HTTP response product.
json_response
Builds a JSON HTTP response product. Call form: http.json_response(status, body).
/ std/net/http
$ res = http.json_response(200, "{\"ok\":true}")Parameters: status: HTTP status code. body: JSON text. Returns: HTTP response product.
parse_request
Parses raw HTTP request bytes into a request product. Call form: http.parse_request(raw).
/ std/net/http
$ req = http.parse_request(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n")
Parameters: data: request bytes. Returns: Request product or failure shape.
format_response
Formats a response product as HTTP bytes. Call form: http.format_response(res).
/ std/net/http
$ b = http.format_response(res)
Parameters: res: HTTP response product. Returns: Response bytes.
dispatch
Dispatches a request to the first matching route handle. Call form: http.dispatch(routes, req).
/ std/net/http
$ res = http.dispatch(req, routes)
Parameters: req: HTTP request. routes: list of path/handle products. Returns: HTTP response product.
handle_connection
Reads one HTTP request from conn, dispatches routes, and writes the response. Call form: http.handle_connection(c, routes).
/ std/net/http
http.handle_connection(conn, routes)
Parameters: conn: TCP connection. routes: list of path/handle products. Returns: None after the response is written.
serve
Serves routes on a TCP address until the process stops. Call form: http.serve(addr, routes).
/ std/net/http
$ health = () {
^ http.text_response(200, "ok")
}
http.serve("127.0.0.1:8080", [{ path: "/health", handle: health }])Parameters: addr: listen address. routes: list of path/handle products. Returns: None under a long-running accept loop.