Skip to content

HTTP

Perform HTTP requests to load and update data remotely and interact with external APIs

Types

HttpOptions

Options for an HTTP request

PropertyTypeDescription
urlstringURL to send the request to
methodstringHTTP request method (“GET” / “POST” / “PUT” …etc)
responseFormat”json” / “text”Response type to get back (“json” for parsed JSON, “text” for plain text)
headersRecord<string, string>Map of header key value pairs
bodyobject / stringBody of the HTTP request (Provide an object and it will be serialized as JSON)
timeoutnumberTimeout in seconds for the request

HttpResponse

Options for an HTTP request

PropertyTypeDescription
statusnumberHTTP response status code (i.e 200)
headersRecord<string, string>Map of header key value pairs on the response
bodyobject / stringJSON object if “responseFormat” is “json” otherwise a string for “text”
okbooleantrue if the response status code is 2xx

Functions

get

Function (url: string, options?: HttpOptions) => Promise<HttpResponse>

Performs an HTTP GET request to the provided URL returns a HttpResponse

ArgumentTypeExampleDescription
urlstringhttps://jsonplaceholder.typicode.com/todos/1URL for the HTTP request
optionsHttpOptionsAdditional request options
const response = await api.http.get(
"https://jsonplaceholder.typicode.com/todos/1",
{
// Specify responseFormat: "json" to parse the response as JSON
responseFormat: "json",
}
);
if (!response.ok) {
// ...Got an error response
return;
}
const body = response.body;
// ...Work with the response

post

Function (url: string, body?: string | object, options?: HttpOptions) => Promise<HttpResponse>

Performs an HTTP POST request to the provided URL returns a HttpResponse

ArgumentTypeExampleDescription
urlstringhttps://jsonplaceholder.typicode.com/todosURL for the HTTP request
bodystring / objectOptional request body, objects will be serialized as JSON
optionsHttpOptionsAdditional request options
const response = await api.http.post(
"https://jsonplaceholder.typicode.com/todos/1",
// JSON payload
{
key: "value",
},
{
// Specify responseFormat: "json" to parse the response as JSON
responseFormat: "json",
}
);
if (!response.ok) {
// ...Got an error response
return;
}
const body = response.body;
// ...Work with the response

put

Function (url: string, body?: string | object, options?: HttpOptions) => Promise<HttpResponse>

Performs an HTTP PUT request to the provided URL returns a HttpResponse

ArgumentTypeExampleDescription
urlstringhttps://jsonplaceholder.typicode.com/todosURL for the HTTP request
bodystring / objectOptional request body, objects will be serialized as JSON
optionsHttpOptionsAdditional request options
const response = await api.http.put(
"https://jsonplaceholder.typicode.com/todos/1",
// JSON payload
{
key: "value",
},
{
// Specify responseFormat: "json" to parse the response as JSON
responseFormat: "json",
}
);
if (!response.ok) {
// ...Got an error response
return;
}
const body = response.body;
// ...Work with the response

patch

Function (url: string, body?: string | object, options?: HttpOptions) => Promise<HttpResponse>

Performs an HTTP PATCH request to the provided URL returns a HttpResponse

ArgumentTypeExampleDescription
urlstringhttps://jsonplaceholder.typicode.com/todosURL for the HTTP request
bodystring / objectOptional request body, objects will be serialized as JSON
optionsHttpOptionsAdditional request options
const response = await api.http.patch(
"https://jsonplaceholder.typicode.com/todos/1",
// JSON payload
{
key: "value",
},
{
// Specify responseFormat: "json" to parse the response as JSON
responseFormat: "json",
}
);
if (!response.ok) {
// ...Got an error response
return;
}
const body = response.body;
// ...Work with the response

request

Function (url: string, body?: string | object, options?: HttpOptions) => Promise<HttpResponse>

Performs an HTTP request returns a HttpResponse.

Use this if you are needing to use an HTTP method like “DELETE” which there is not a builtin function for.

ArgumentTypeExampleDescription
optionsHttpOptionsAdditional request options
const response = await api.http.request({
url: "https://jsonplaceholder.typicode.com/todos/1",
method: "DELETE",
// Specify responseFormat: "json" to parse the response as JSON
responseFormat: "json",
});
if (!response.ok) {
// ...Got an error response
return;
}
const body = response.body;
// ...Work with the response