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.
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 | |
|---|---|
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