Skip to content
Docs
Modules
http

http

The http module provides functions for making HTTP requests and defines request and response types.

ℹ️

Using the fetch built-in function instead of this module is encouraged. This module provides more low-level control of how HTTP requests are built and sent, which may be useful in some situations.

The approach used to make an HTTP request is as follows:

Create a Request

req := http.get("https://api.ipify.org")

Send the Request

res := req.send()

Handle the Response

print("response status:", res.status, "text:", res.text())

Functions

get

Function signature
get(url string, headers map, params map) request

Creates a new GET request with the given URL, headers, and query parameters. The headers and query parameters are optional. The request that is returned can then be executed using its send method. Read more about the request type below.

Example
>>> http.get("https://api.ipify.org").send()
http.response(status: "200 OK", content_length: 14)

delete

Function signature
delete(url string, headers map, params map) request

Creates a new DELETE request with the given URL, headers, and query parameters. The headers and query parameters are optional.

head

Function signature
head(url string, headers map, params map) request

Creates a new HEAD request with the given URL, headers, and query parameters. The headers and query parameters are optional.

listen_and_serve

Function signature
listen_and_serve(addr string, handler func(w response_writer, r request))

Starts an HTTP server that listens on the specified address and calls the handler function to handle requests. As a convenience, the handler function may return a map or list object to be marshaled as JSON, or a string or byte slice object which will be written as the response body as-is.

listen_and_serve_tls

Function signature
listen_and_serve_tls(addr, cert_file, key_file string, handler func(w response_writer, r request))

Acts the same as listen_and_serve, but uses the provided certificate and key files to work over HTTPS.

patch

Function signature
patch(url string, headers map, body byte_slice) request

Creates a new PATCH request with the given URL, headers, and request body. The headers and request body parameters are optional.

post

Function signature
post(url string, headers map, body byte_slice) request

Creates a new POST request with the given URL, headers, and request body. The headers and request body parameters are optional.

put

Function signature
put(url string, headers map, body byte_slice) request

Creates a new PUT request with the given URL, headers, and request body. The headers and request body parameters are optional.

request

Function signature
request(url string, options map) request

Creates a new request with the given URL and options. If provided, the options map may contain any of the following keys:

NameTypeDescription
methodstringThe HTTP method to use.
headersmapThe headers to send with the request.
paramsmapThe query parameters to send with the request.
bodybyte_slice or readerThe request body.
timeoutintRequest timeout in milliseconds.
dataobjectObject to marshal as JSON and send in the request body

If both body and data are provided, the body value will be used.

Types

request

Represents an HTTP request that is being built and sent.

Attributes

NameTypeDescription
urlstringThe URL of the request.
content_lengthintThe length of the request body.
headermapThe headers of the request.
sendfunc()Sends the request.
add_headerfunc(key string, value object)Adds a header to the request.
add_cookiefunc(key string, value map)Adds a cookie to the request.
set_bodyfunc(body byte_slice)Sets the request body.
set_datafunc(data object)Request body as an object to marshal to JSON

response

Represents an HTTP response.

Attributes

NameTypeDescription
statusstringThe status of the response.
status_codeintThe status code of the response.
protostringThe protocol of the response.
content_lengthintThe length of the response body.
headermapThe headers of the response.
cookiesmapThe cookies of the response.
responseobjectThe response body.
jsonfunc() objectThe response body as JSON.
textfunc() stringThe response body as text.
closefunc()Closes the response body.

response_writer

Represents an HTTP response writer.

Attributes

NameTypeDescription
add_headerfunc(key, value string)Adds a header to the header map that will be sent.
del_headerfunc(key string)Deletes a header from the header map that will be sent.
writefunc(object)Writes the object as the HTTP reply.
write_headerfunc(status_code int)Sends an HTTP response header with the provided status code.