# HTTP request

The block sends a request to a third-party server and handles the response: shows it in the chat, saves it to variables or passes it to your code.

![Settings panel of the "HTTP request" block](https://support.watbot.org/_media/en/01-chatbots/03-blocks/06-developer/_images/http.webp?v=1791781230)

## Settings

- **"URL"** — the request address, up to 2048 characters. The address must start with `http://` or `https://`.
- **"Method"** — `GET`, `POST`, `PUT`, `DELETE` or `PATCH`.
- **"Headers"** — "name = value" pairs, up to ten. The "Add header" button adds a row. If the `Accept` header is not set, the block sends `Accept: application/json`.
- **"Body"** — the request body. The field appears for every method except `GET`. The format is chosen from a list:
  - `JSON` — the whole body is written in the editor;
  - `x-www-form-urlencoded` — "key = value" pairs; rows are added with the "Add string" button;
  - `multipart/form-data` — the same pairs plus files. The "Add file" button offers two options: "Link to file or variable" — the bot downloads the file from the link and attaches it to the request; "Local file" — the file is uploaded from your computer. One request carries no more than 20 files.
- **"Timeout"** — how long to wait for the server's response, in seconds, from 1 to 60. The default is 5.
- **"Send contact details in a request"** — adds three fields to the request: `bot_id` (the bot ID), `contact` (the contact's data) and `variables` (the contact's variables). For requests with a body, the fields go into the body. For `GET`, they go into the query string, and only if the address already has at least one parameter.
- **"Display response body in chat"** — the bot sends the server's response to the contact as is. The switch is on by default; turn it off if the response is technical.
- **"Write the response code to a variable"** — the response code (`200`, `404` and so on) is saved to the contact variable named in the "Variable" field.
- **"Write json response to variables"** — spreads a JSON response across contact variables. In the "Relationship of variables" block, enter the dot-separated path to the value on the left and the variable name on the right. For example, `data.product.name` → `product`. An array element is taken by its number: `items.0.price`. If there is no value at that path, an empty string is written to the variable.

- **"Request server"** — where the request is sent from: "Default", "Europe" or "SOCKS5 proxy". For your own proxy, fill in "Host" and "Port", and if the proxy requires a login — "User" and "Password".

- **"Handle response with JavaScript"** — opens the code editor. The code receives the server's response in the `response` object and can do everything the code of the "JavaScript interpreter" block can. See [Processing the response with code](https://support.watbot.org/chatbots/blocks/developer/http#processing-the-response-with-code) for details.

Further down in the settings are the usual "Menu" and "Advanced" sections — the same as in blocks with a message.

![Response code goes to a variable, JSON response is split into variables: order.id and order.status](https://support.watbot.org/_media/en/01-chatbots/03-blocks/06-developer/_images/http-response.webp?v=1791781230)

## Inserting data

[Templates](https://support.watbot.org/chatbots/templates) work in the address, header values, the request body and the paths in "Relationship of variables":

```
https://example.com/api/orders?phone={{ phone }}&city={{ $city }}
```

In a `JSON` body, templates are filled in before the body is parsed. If the result is invalid JSON — for example, a variable value contained a quotation mark — the request is sent with an empty body, and the `Invalid JSON` error appears in the "Execution log".

![A POST request with an Authorization header and a JSON body with templates](https://support.watbot.org/_media/en/01-chatbots/03-blocks/06-developer/_images/http-body.webp?v=1791781230)

## Outputs

- Menu items — if you added them in "Menu", the bot waits for the person's reply, as in a regular block with buttons.
- "Any other phrase" — the person wrote something that is not in the menu.
- "No response from server" — the server did not respond within the time set in the "Timeout" field, the connection could not be established or the request failed.
- "Continue" — the scenario moves on right after the request, without waiting for the person's reply.

![The "HTTP request" block on the scheme: "No response from server", "Any other phrase" and "Continue" outputs](https://support.watbot.org/_media/en/01-chatbots/03-blocks/06-developer/_images/http-outputs.webp?v=1791781230)

> [!IMPORTANT]
> A response with an error code — for example, `404` or `500` — is a normal response for the block: the "No response from server" output will not fire. Write the response code to a variable and check it in the next block.

## Limits

- Requests to internal addresses and to the platform's own addresses are forbidden: such a request is not sent, and the `URL is not valid` error is left in the "Execution log".
- The block follows no more than five redirects in a row.
- No more than 10 requests per second go to one domain from the whole platform. Extra requests wait in a queue.
- "Relationship of variables" holds up to 300 rows.

## Processing the response with code

The code from the "Handle response with JavaScript" field runs after the block's other settings. The server's response is in the `response` object:

| Field | What it holds |
|---|---|
| `response.status` | The response code, a number |
| `response.body` | The response body as a string |
| `response.data` | The parsed body, if the server responded with JSON |

See [Built-in variables](https://support.watbot.org/api/javascript/builtins#server-response) for details.

```js
if (response.status === 200 && response.data.paid) {
    setContactVariable('paid', 'yes');
} else {
    sendMessage('The payment has not arrived yet.');
    disableContinue();
}
```

The functions and limits are the same as in the "JavaScript interpreter" block. If the code ends with an error, its text is sent to the chat, and the "Continue" connection does not fire.

!["Process the response with JavaScript" is on: the code checks the response code and sends a message](https://support.watbot.org/_media/en/01-chatbots/03-blocks/06-developer/_images/http-js.webp?v=1791781230)
