# Fetch

> How Elide supports fetch, Request, Response, Headers, FormData, Blob and File: supported URL schemes, no CORS or HTTP cache, the default User-Agent and response decompression.

`fetch`, `Blob`, `File`, `Headers`, `FormData`, `Request`, and `Response` are globals with the
[Fetch Standard](https://fetch.spec.whatwg.org/) API: bodies, streams, `clone()`, `Response.json()`, `AbortSignal`
and subresource integrity.

## Example

```js
// fetch.mjs
const form = new FormData();
form.append("name", "elide");
form.append("file", new File(["hello"], "hello.txt", { type: "text/plain" }));

const request = new Request("https://example.com/upload", { method: "POST", body: form });
console.log(request.method, request.headers.get("content-type").split(";")[0]);

const response = Response.json({ ok: true }, { status: 201, headers: { "x-id": "7" } });
console.log(response.status, response.headers.get("x-id"), JSON.stringify(await response.json()));

const blob = new Blob(["a", "b", "c"], { type: "text/plain" });
console.log(blob.size, await blob.slice(1).text());
```

`elide run fetch.mjs` prints:

```text
POST multipart/form-data
201 7 {"ok":true}
3 bc
```

## Differences from browsers

- `fetch` accepts `http:` and `https:` URLs; `data:` and `blob:` URLs reject with `unsupported URL scheme`.
- There is no origin: `mode`, `credentials`, `referrer` and `keepalive` are accepted and ignored, and no CORS
  checks, preflights, cookies or `Referer` headers are applied.
- Requests send `User-Agent: Elide/<version>` and `Accept: */*` unless you set those headers.
  `ELIDE_HTTP_USER_AGENT` changes the default user agent; its value must be printable ASCII.
- Gzip, deflate, Brotli, and Zstandard responses are decoded; response headers still describe the encoded body.
- An explicit `Accept-Encoding` header is sent as given; range requests default to `identity`.
- `fetch(url, { decompress: false })` sends `Accept-Encoding: identity` and returns any encoded body unchanged.
- A malformed compressed body rejects.
- There is no HTTP cache: conditional headers are sent as given, and a `304` has headers and no body.
- `Headers` applies the request, response, no-CORS and immutable guards; iteration lowercases and sorts names and
  keeps `Set-Cookie` values separate.
- Integrity checks buffer up to 100 MiB before the response resolves.
- HTTP/1.1, HTTP/2, and HTTP proxies (including HTTPS `CONNECT`) work; authenticated proxies fail.
- Certificates and hostnames are validated; for trust settings, see
  [Runtime Environment Variables](https://elide.help/docs/runtime-env-variables).

To serve HTTP, see [`Elide.serve`](https://elide.help/docs/api-globals-elide) or [node:http](https://elide.help/docs/api-node-http).

## Samples

- [Local server and fetch](https://elide.help/docs/code-samples#local-server-and-fetch)
- [HTTP handler](https://elide.help/docs/code-samples#http-handler)

<!-- api-reference:start af4506a7c6a2 -->
## Reference

**Status:** Full · 1,455 of 2,511 suite tests pass (57.9%) · since 1.1.0

### Exports

| Export | Kind | Status | Notes |
| --- | --- | --- | --- |
| `Headers` | class | Full | `append()`, `delete()`, `get()`, `getSetCookie()`, `has()`, `set()`, `iteration` |
| `Request` | class | Full | `method`, `url`, `headers`, `destination`, `referrer`, `referrerPolicy`, `mode`, `credentials`, `cache`, `redirect`, `integrity`, `keepalive`, `signal`, `duplex`, `clone()`, `body`, `bodyUsed`, `arrayBuffer()`, `blob()`, `bytes()`, `formData()`, `json()`, `text()` |
| `Response` | class | Full | `Response.error()`, `Response.redirect()`, `Response.json()`, `type`, `url`, `redirected`, `status`, `ok`, `statusText`, `headers`, `clone()`, `body`, `bodyUsed`, `arrayBuffer()`, `blob()`, `bytes()`, `formData()`, `json()`, `text()` |
| `Blob` | class | Full | `size`, `type`, `slice()`, `text()`, `arrayBuffer()`, `bytes()`, `stream()` |
| `File` | class | Full | `name`, `lastModified` |
| `FormData` | class | Full | `append()`, `delete()`, `get()`, `getAll()`, `has()`, `set()`, `iteration` |

**Tests:** [wpt-wintertc `fetch`](https://github.com/elide-dev/testsuite/blob/main/reports/1.6.0%2B6e718933f/4cbcae6c33a8/wpt-wintertc/wpt-wintertc.md): 1,455 pass, 1,043 fail, 13 error, 400 skipped. Failing: `fetch/api/basic/request-forbidden-headers.any.js` (96), `fetch/api/redirect/redirect-referrer-override.any.js` (91), `fetch/data-urls/processing.any.js` (68), `fetch/api/headers/header-values-normalize.any.js` (57), `fetch/api/cors/cors-keepalive.any.js` (51), `fetch/api/cors/cors-preflight-star.any.js` (34), `fetch/api/redirect/redirect-origin.any.js` (30), `fetch/api/cors/cors-preflight-status.any.js` (27), and 110 more.
<!-- api-reference:end -->
