Http request

2 snippets across 2 stacks - Bash & Linux, JavaScript

SHBash & Linux

Transfer Data with URLs

SH · Networking
Syntax
curl [options] URL
Example
curl https://api.example.com/users
curl -X POST -H 'Content-Type: application/json' -d '{"name":"Ada"}' https://api.example.com/users
curl -o release.tar.gz -L https://github.com/proj/repo/archive/v2.1.tar.gz

Note -o saves to a file, -O uses the remote filename. -L follows redirects (essential for GitHub downloads). -s silences progress bar. -I fetches only headers. -k skips TLS verification (use only for debugging, never in production).

JSJavaScript

Fetch API

JS · Async Programming
Syntax
const response = await fetch(url, options);
Example
// GET request
const res = await fetch("/api/products");
const products = await res.json();

// POST request
const created = await fetch("/api/products", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Widget", price: 29.99 }),
});

if (!created.ok) throw new Error(`HTTP ${created.status}`);

Note fetch() does NOT reject on HTTP error status codes (404, 500). Always check response.ok or response.status. The body can only be consumed once.

Frequently asked questions

How does Bash & Linux handle http request?
This task is covered in 2 stacks on this page: Bash & Linux, JavaScript. The "Transfer Data with URLs" snippet in Bash & Linux uses `curl [options] URL`.
Which command does the Bash & Linux example use?
The "Transfer Data with URLs" snippet uses `curl [options] URL`, from the Networking section of the Bash & Linux cheat sheet.
Which stacks cover "http request" on this page?
Bash & Linux, JavaScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Transfer Data with URLs": -o saves to a file, -O uses the remote filename. -L follows redirects (essential for GitHub downloads). -s silences progress bar. -I fetches only headers. -k skips TLS verification (use only for debugging, never in production).