Skip to content

HTTP

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

Options for an HTTP request

Property Type Description
url string URL to send the request to
method string HTTP request method (“GET” / “POST” / “PUT” …etc)
responseFormat “json” / “text” Response type to get back (“json” for parsed JSON, “text” for plain text)
headers Record<string, string> Map of header key value pairs
body object / string Body of the HTTP request (Provide an object and it will be serialized as JSON)
timeout number Timeout in seconds for the request

Options for an HTTP request

Property Type Description
status number HTTP response status code (i.e 200)
headers Record<string, string> Map of header key value pairs on the response
body object / string JSON object if “responseFormat” is “json” otherwise a string for “text”
ok boolean true if the response status code is 2xx

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

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

Argument Type Example Description
url string https://jsonplaceholder.typicode.com/todos/1 URL for the HTTP request
options HttpOptions Additional 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

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

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

Argument Type Example Description
url string https://jsonplaceholder.typicode.com/todos URL for the HTTP request
body string / object Optional request body, objects will be serialized as JSON
options HttpOptions Additional 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

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

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

Argument Type Example Description
url string https://jsonplaceholder.typicode.com/todos URL for the HTTP request
body string / object Optional request body, objects will be serialized as JSON
options HttpOptions Additional 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

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

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

Argument Type Example Description
url string https://jsonplaceholder.typicode.com/todos URL for the HTTP request
body string / object Optional request body, objects will be serialized as JSON
options HttpOptions Additional 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

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.

Argument Type Example Description
options HttpOptions Additional 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