Skip to content

web.post ​

Sends an HTTP POST request to a URL.

js
web.post(url, headers, body, base64Body)

Parameters ​

ParameterTypeDescription
urlstringThe URL to send the request to.
headersobjectOptional. The request headers, keyed by header name.
bodyobject | stringOptional. The request body. An object is sent as JSON, and a string is sent as it is.
base64BodybooleanOptional. true to always return the response body base64-encoded. Defaults to false.

Returns ​

object

The response:

  • status (number): the HTTP status code, or 0 when no response was received.
  • headers (array): the response headers, each an object with one key/value pair.
  • body (string): the response body.
  • error (string): why the request failed, when no response was received.

Binary responses

Common binary types, such as images, PDFs and audio, are detected and returned base64-encoded. To save a file, use web.download.

Status and errors

A 4xx or 5xx status means the server received the request and returned an error. See HTTP status codes.

error is only set when the request fails before a response comes back. status is then usually 0. This can happen when:

  • the URL is invalid or malformed.
  • the connection or response times out.
  • the network fails, such as a lost connection, a DNS problem, an SSL validation error or a blocked request.
  • the server drops or rejects the request without responding.

Examples ​

js
const requestBody = { "name": "Example User", "email": "user@example.com" };
const response = web.post("https://api.example.com/users", { "Content-Type": "application/json" }, requestBody);

// Check if the response status code is in the success range (between 200 and 299, inclusive).
if (response.status >= 200 && response.status < 300) {
    console.log("Request succeeded.");
    console.log(response.body);
} else if (response.error) {
    // An error occurred while sending the request (e.g., network error, timeout, etc.).
    console.log("Error occurred while sending the request: " + response.error);
} else {
    // The request failed with a non-success status code.
    console.log("Request failed with status code: " + response.status);
    console.log(response.body);
}