Glossary
Fetch API: the standard HTTP call in JavaScript
The Fetch API is the web standard for making HTTP requests from JavaScript, available in every modern browser and built into Node.js since version 18.
The shape of a call
fetch(url, options) returns a promise that resolves to a Response once the headers arrive. You then read the body as text, JSON, a blob or a stream. Options cover the method, headers, body, credentials mode, redirect behavior and an abort signal.
const reply = await fetch("https://api.example.com/v1/products?limit=50", {
headers: { Accept: "application/json" },
signal: AbortSignal.timeout(15000),
});
if (!reply.ok) throw new Error(`HTTP ${reply.status}`);
const catalog = await reply.json();
Two surprises for newcomers
First, fetch does not reject on HTTP errors. A 404 or 503 resolves normally with ok set to false; only network failures and aborts reject. Always check response.ok or the status. Second, there is no timeout option. Use AbortSignal.timeout(ms), as above, or an AbortController you abort yourself, otherwise a stalled server can keep the promise pending for a very long time (see timeout).
Fetch in the browser
In a page, fetch is bound by the same origin policy and CORS. A script on one site cannot read responses from another unless the server allows it. Cookies are sent according to the credentials option. This is the context in which a site's own front end calls its internal JSON endpoints, and also why a browser extension or page script cannot freely scrape other domains.
Fetch in Node.js
Node's fetch is built on the undici HTTP client. CORS does not apply on a server, cookies are not stored automatically (you keep and send them yourself), and you can tune connection pooling and proxies through undici's dispatcher options. Deno and Bun ship fetch too, with small differences worth checking in their documentation. As with other non browser clients, the TLS handshake and header set differ from a real browser, so protected sites may challenge server side fetch more readily.
Requests with a body
For JSON, stringify the body yourself and declare the content type, either as a header or by wrapping the string in a typed Blob as shown below; a plain string is sent as text. For forms, pass a URLSearchParams or FormData object and fetch sets the header. This is the difference between a POST request with a JSON body and one with form encoding.
Streaming large responses
The response body is a stream, which lets you process large downloads without holding them fully in memory. Read response.body chunk by chunk, or pipe it to a file in Node.js. For scraping, this is handy with big JSON exports or sitemaps; for small API replies, the plain json() method is simpler.
Fetch and a solving API
Calling GateSolver needs nothing beyond fetch. Create a task with a POST, then poll for the result.
const post = (path, payload) => fetch(`https://gatesolver.com${path}`, {
method: "POST",
// a typed Blob makes fetch send the JSON content type for us
body: new Blob([JSON.stringify({ clientKey: "YOUR_API_KEY", ...payload })],
{ type: "application/json" }),
}).then((r) => r.json());
const { taskId } = await post("/createTask", { task: {
type: "AntiAwsWafTaskProxyLess", websiteURL: "https://shop.example/" } });
Poll /getTaskResult with that taskId every three seconds until the status reads ready. An AWS WAF task returns a token you send as the aws-waf-token cookie on the following requests, typically under 3 seconds. A task that fails is not billed. The API docs list the fields for every type.
Common questions
Does fetch reject on a 404 or 500 status?
No. It resolves with ok set to false. Only network errors and aborted requests reject the promise.
How do I add a timeout to fetch?
Pass signal: AbortSignal.timeout(ms), or abort an AbortController yourself after a delay.
Is fetch available in Node.js without a package?
Yes, it is built in since Node.js 18 and is based on the undici client.
Does Node.js fetch keep cookies between requests?
No. You read Set-Cookie headers and send a Cookie header yourself, or use a cookie jar helper.
How do I send JSON with fetch?
Set the Content-Type header to application/json and pass JSON.stringify of your object as the body.
Keep reading
- AxiosA promise based HTTP client for Node.js and browsers with interceptors and instances, often the fetch layer of JavaScript scrapers.
- POST requestThe HTTP method for sending data in a request body, with JSON and form encodings compared and createTask as an example.
- Asynchronous APIAPIs that accept a job now and deliver the result later, like createTask with getTaskResult, and how to poll them well.
- API docscreateTask, getTaskResult, getBalance, error codes and the in.php format.