Skip to content

Http Client

Spice
import "std/net/http-client";

HttpClient struct

A blocking HTTP/1.1 client, built on the plain TCP sockets of "std/net/socket".

Every request opens its own connection, announces Connection: close and closes the connection again once the response has been read, so no connection state survives a call. Responses framed by Content-Length, by the chunked transfer coding, or by the connection close of an HTTP/1.0 server are all understood.

There is no TLS underneath, so https URLs are rejected instead of being silently downgraded. Use the libcurl bindings in "std/bindings/libcurl" when a request has to go over https.

Fields

Name Type Description
defaultHeaders HttpHeaders Fields that are added to every request that lacks them
timeoutMillis unsigned long Per read/write timeout, 0 to block indefinitely
maxMessageSize unsigned long Upper bound on the size of a response
maxRedirects unsigned int Number of redirects to follow, 0 to hand 3xx back as-is

Constructors

ctor

Spice
public p HttpClient.ctor()

Constructs a client with the default timeout, message size limit and redirect budget

Methods

setDefaultHeader

Spice
public p HttpClient.setDefaultHeader(string name, string value)

Adds or replaces a header field that is sent with every request

Parameters

Name Type Description
name string Field name
value string Field value

get

Spice
public f<Result<HttpResponse>> HttpClient.get(string url)

Performs a GET request

Parameters

Name Type Description
url string Absolute URL to request

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

Spice
public f<Result<HttpResponse>> HttpClient.head(string url)

Performs a HEAD request, which yields the head of the response without its body

Parameters

Name Type Description
url string Absolute URL to request

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

post

Spice
public f<Result<HttpResponse>> HttpClient.post(string url, const String& body, string contentType = CONTENT_TYPE_TEXT)

Performs a POST request

Parameters

Name Type Description
url string Absolute URL to request
body const String& Payload to send
contentType string Media type of the payload (default: CONTENT_TYPE_TEXT)

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

put

Spice
public f<Result<HttpResponse>> HttpClient.put(string url, const String& body, string contentType = CONTENT_TYPE_TEXT)

Performs a PUT request

Parameters

Name Type Description
url string Absolute URL to request
body const String& Payload to send
contentType string Media type of the payload (default: CONTENT_TYPE_TEXT)

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

patch

Spice
public f<Result<HttpResponse>> HttpClient.patch(string url, const String& body, string contentType = CONTENT_TYPE_TEXT)

Performs a PATCH request

Parameters

Name Type Description
url string Absolute URL to request
body const String& Payload to send
contentType string Media type of the payload (default: CONTENT_TYPE_TEXT)

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

delete

Spice
public f<Result<HttpResponse>> HttpClient.delete(string url)

Performs a DELETE request

Parameters

Name Type Description
url string Absolute URL to request

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

request

Spice
public f<Result<HttpResponse>> HttpClient.request(HttpMethod method, string url, const String& body, string contentType = CONTENT_TYPE_TEXT)

Performs a request with the given method, following redirects along the way

Parameters

Name Type Description
method HttpMethod Request method
url string Absolute URL to request
body const String& Payload to send, may be empty
contentType string Media type of the payload (default: CONTENT_TYPE_TEXT)

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed

send

Spice
public f<Result<HttpResponse>> HttpClient.send(const HttpRequest& request, const String& url)

Sends a prepared request to the given URL, following redirects along the way.

The request target, the Host field and the fields of defaultHeaders are filled in from the URL and the client configuration, so the caller only has to provide method, body and any extra header fields it cares about.

Parameters

Name Type Description
request const HttpRequest& Request to send
url const String& Absolute URL to send it to

Returns: Result<HttpResponse> — The response, or an error describing why the exchange failed