curl · fetch Recipes

Common HTTP tasks — JSON requests, auth, file upload, retries — as ready-to-use curl and fetch code.

Advanced

Retry on 429 with backoff

When you hit 429 Too Many Requests (rate limiting), wait and retry with exponential backoff instead of hammering the server.

Set a request timeout

Put a timeout on a request so you never hang forever on a slow or unresponsive server. Most defaults are effectively infinite, so set one explicitly.

Request a compressed response

Request a compressed response via `Accept-Encoding` to cut transfer size dramatically — especially effective for large JSON and text responses.

Consume Server-Sent Events

Consume Server-Sent Events (SSE, `text/event-stream`), where the server pushes a continuous stream of events down a single open connection.

Send a GraphQL query

POST a query and its variables to a GraphQL endpoint to fetch exactly the fields you need in a single request.

Auth

Send a Bearer token

Send an access token issued by OAuth 2.0, JWT, or similar in the `Authorization: Bearer` header. This is the most common API authentication scheme today.

Use Basic authentication

HTTP Basic authentication sends your username and password as a base64-encoded `user:pass` string.

Caching

Conditional GET with ETag

A conditional GET: send back the ETag you received earlier via `If-None-Match`, and if the resource is unchanged you get just a 304 Not Modified, saving bandwidth.

Resume a download with Range

Resume an interrupted download by requesting only the bytes after what you already have, using the `Range` header and 206 Partial Content.

Debugging

Inspect response headers

Check the status code and response headers without downloading the body — handy for auditing cache policy, content type, and redirects.

Debug a request verbosely

Print the actual request/response headers and even the TLS handshake so you can see exactly what is happening and why.

Skip TLS verification for local dev

Skip TLS certificate verification to reach a local dev server using a self-signed cert. Use this in development only.

Trigger a CORS preflight

Reproduce the CORS preflight (OPTIONS) that a browser sends before a cross-origin request, using curl to verify the server's CORS configuration.

Measure request timing

Measure how long each phase of a request takes (DNS, connect, TLS, first byte, total) to pinpoint where latency comes from.

Files

Upload a file with multipart/form-data

Upload a file alongside text fields using `multipart/form-data` — the same mechanism a browser file-upload form uses.

Download a file to disk

Fetch a remote file and save it to disk. Works for images, PDFs, binaries — any file type.

Requests

Fetch JSON with GET

The most fundamental recipe: issue a GET request and parse the JSON body the server returns. It is read-only and carries no request body.

Send a JSON POST

POST a JSON body to create a new resource. On success you typically get back 201 Created plus the resource that was created.

Replace a resource with PUT

Use PUT to replace a resource wholesale with a new representation. The body you send becomes the resource's complete new state.

Partially update with PATCH

Use PATCH to change only some fields of a resource. Send just the fields you want to modify; everything else is left untouched.

Delete a resource

Remove the target resource with the DELETE method. On success you usually get a bodyless 204 No Content.

Follow redirects

How to follow a 3xx response's `Location` header to the final destination, and conversely how to observe redirects without following them.

Send custom headers

Attach arbitrary custom headers to a request — useful for request-tracing IDs, API versions, language preferences, and more.

Send and store cookies

Store cookies the server sends and automatically replay them on later requests — essential for session-based login flows.

Submit a URL-encoded form

Send form data as `application/x-www-form-urlencoded` — the classic HTML form encoding, also used by OAuth token endpoints and similar APIs.