--- title: http section: http order: 120 --- ## `http.request(options)` Curl-like request giving full control over the method, headers and body. Returns a response object `{ok, status, statusText, headers, body}` for a reply with any status code (`ok` is true for 2xx; `body` is base64 when `responseType` is `"base64"`). A request that gets no reply (bad options, DNS, refused, timeout) throws `HTTPError`. Supported `options`: | Field | Description | |-------|-------------| | `url` | Target URL (required) | | `method` | HTTP method, default `GET` (case-insensitive) | | `headers` | Object of request headers to set, e.g. `{"Authorization":"Bearer x"}` | | `body` | Raw text request body | | `json` | Object sent as a JSON body (sets `Content-Type: application/json`) | | `form` | Object sent as `application/x-www-form-urlencoded` | | `bodyBase64` | Binary request body, base64 encoded (sets `application/octet-stream`) | | `contentType` | Override the `Content-Type` header | | `username` / `password` | HTTP basic auth credentials | | `timeout` | Timeout in seconds (`0` / omitted = no timeout) | | `followRedirect` | Follow 3xx redirects (default `true`) | | `responseType` | `"text"` (default) or `"base64"` for binary responses | Body precedence when several are supplied: `bodyBase64` > `form` > `json` > `body`. ```javascript var resp = http.request({ url: "https://example.com/api", method: "POST", headers: {"Authorization": "Bearer TOKEN"}, json: {a: 1, b: 2} }); if (resp.ok){ console.log(resp.status, resp.body); } ``` Convenience helpers built on `http.request` (all return the response object): `http.put(url, body, headers, contentType)`, `http.patch(url, body, headers, contentType)`, `http.delete(url, headers)`, `http.postForm(url, formObject, headers)` and `http.postJSON(url, object, headers)`. ## `http.get(url, headers)` `headers` is optional. Returns the response body string; throws `HTTPError` when the request gets no reply (bad URL, DNS, refused, timeout). ```javascript var body = http.get("https://example.com"); var body2 = http.get("https://example.com", {"Authorization": "Bearer x"}); ``` ## `http.post(url, body, headers, contentType)` `headers` and `contentType` are optional. Without them the body is sent as JSON (backward compatible); with them the given headers / content type are used. Returns the response body string. ```javascript var body = http.post("https://example.com/api", JSON.stringify({a:1})); ``` ## `http.head(url, headerKey)` - Without `headerKey`: returns JSON string of all headers. - With `headerKey`: returns JSON string of that header value. ```javascript var headers = JSON.parse(http.head("https://example.com")); ``` ## `http.getCode(url)` Returns status code. ```javascript var code = http.getCode("https://example.com"); ``` ## `http.download(url, destDirVpath, filenameOptional)` Downloads into destination directory. ```javascript http.download("https://example.com/a.zip", "user:/Downloads", "a.zip"); ``` ## `http.getb64(url)` ```javascript var raw = http.getb64("https://example.com/logo.png"); ``` ## `http.redirect(targetUrl, statusCode)` Default status code is `307` when omitted. ```javascript http.redirect("https://example.com/new", 302); ```