Menu
Sending data
“Query strings, HTML forms and JSON bodies.”
There are three common ways to send data to a server.
1. In the URL: query strings#
Good for filters, search terms and pagination on GET requests:
GET /products?category=books&sort=price&page=2 HTTP/1.1
Host: shop.example.comWarning
URLs end up in browser history, server logs and analytics. Never put passwords or tokens in a query string.
2. In the body: HTML forms#
A plain HTML form sends its fields in the body, encoded like a query string:
1<form method="post" action="/login">
2 <input name="email" type="email">
3 <input name="password" type="password">
4 <button>Log in</button>
5</form>Submitting it sends:
1POST /login HTTP/1.1
2Host: example.com
3Content-Type: application/x-www-form-urlencoded
4Content-Length: 41
5
6email=amin%40example.com&password=hunter2For file uploads, the form uses enctype="multipart/form-data", which splits the body into parts so binary files fit.
3. In the body: JSON#
APIs usually send JSON. The Content-Type header tells the server how to read the body:
1POST /api/todos HTTP/1.1
2Host: api.example.com
3Content-Type: application/json
4Content-Length: 43
5
6{"title": "Learn HTTP", "completed": false}From the terminal:
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"title": "Learn HTTP", "completed": false}'From JavaScript:
1const res = await fetch("/api/todos", {
2 method: "POST",
3 headers: { "Content-Type": "application/json" },
4 body: JSON.stringify({ title: "Learn HTTP", completed: false }),
5});Which one when?#
| You want to… | Use |
|---|---|
| Filter or search, shareable as a link | Query string on GET |
| Submit a simple HTML form | application/x-www-form-urlencoded |
| Upload files | multipart/form-data |
| Talk to an API | application/json |
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(http): Sending data"
[main 8d5bc68] learn(http): Sending data
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: