Http Client¶
| Spice | |
|---|---|
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.
https URLs are carried over a TLS connection (see "std/net/tls"), with the server's certificate and host name verified on every request. trustedCaFile points that verification at a specific CA bundle instead of the operating system's default trust store, e.g. to talk to a server with a self-signed certificate in tests.
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 |
trustedCaFile |
String |
PEM file of trusted CAs for https, "" for the OS default store |
Constructors¶
ctor¶
| Spice | |
|---|---|
Constructs a client with the default timeout, message size limit and redirect budget
Methods¶
setDefaultHeader¶
| Spice | |
|---|---|
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 | |
|---|---|
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
head¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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