What You'll Learn
- What the request body and response body are
- When a body is required, optional, or forbidden
- How the body relates to the
Content-Typeheader - How to read and parse a response body in code
Why This Matters
The body is where the actual data lives. Headers tell the server and client how to handle the message; the body is the message itself. If you cannot read a response body or build a request body correctly, you cannot call or test any API that creates, updates, or returns data.
Simple Explanation
In an HTTP message:
- The start line and headers describe the message.
- The body is the actual content being sent.
Think of it like a parcel: the outside has the address and shipping label (start line + headers); the inside has the item you're sending (body).
Request Body
The request body is what the client sends to the server. It is used to send data for creation, updates, or complex queries.
When a request body is required
| Method | Body required? |
|---|---|
| GET | No — should not have one |
| POST | Usually yes — the data to create |
| PUT | Yes — the full new state |
| PATCH | Yes — the fields to update |
| DELETE | Usually no |
Example POST request with a body
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 56
{"name": "Anita", "email": "anita@example.com", "age": 28}
The body is the JSON line at the bottom. Content-Type tells the server it is JSON; Content-Length says it is 56 bytes long.
Common body formats
| Content-Type | Body format | Use case |
|---|---|---|
application/json | JSON | The default for modern APIs |
application/x-www-form-urlencoded | key=value&key=value | HTML forms |
multipart/form-data | Boundary-separated parts | File uploads |
text/plain | Plain text | Simple text payloads |
application/xml | XML | Legacy and SOAP APIs |
Response Body
The response body is what the server sends back. Even errors have a body — usually a JSON object describing what went wrong.
Example response with a body
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 62
{"id": 123, "name": "Anita", "email": "anita@example.com"}
When the response body is empty
Sometimes there is no body — the status code says everything:
- 204 No Content — Success, but nothing to return (typical for DELETE).
- 304 Not Modified — Use the cached version.
- HEAD requests — Return headers only, never a body.
How to Read a Response Body in Code
JavaScript (browser fetch)
const response = await fetch("https://api.example.com/users/123");
const user = await response.json(); // parses JSON body
console.log(user.name); // "Anita"
JavaScript (Node.js 18+ with native fetch)
const response = await fetch("https://api.example.com/users/123");
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const user = await response.json();
console.log(user.name);
Python (requests library)
import requests
response = requests.get("https://api.example.com/users/123")
user = response.json()
print(user["name"])
cURL
curl https://api.example.com/users/123
cURL prints the body to your terminal by default. Add -i to see headers too.
Sending a Request Body with cURL
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Anita","email":"anita@example.com"}'
-d tells cURL to send the next argument as the body. cURL automatically sets Content-Type: application/x-www-form-urlencoded unless you override it with -H. For JSON, always set the header explicitly.
Common Mistakes
- Forgetting
Content-Typewhen sending a body. The server does not know how to parse the body without it. - Sending a body with GET. Some servers reject it; many proxies strip it. Use query parameters instead.
- Not handling the case where the body is empty. Calling
response.json()on a 204 response throws an error because there is no JSON to parse. - Sending JSON with trailing commas or comments. JSON does not allow either. Use a validator — covered in lesson 14.
- Sending file uploads as JSON. Files use
multipart/form-data, not JSON. Trying to encode a file as JSON is slow and breaks large files.
Practical Exercise (10 minutes)
- Use cURL to GET a post from JSONPlaceholder and look at the body:
curl https://jsonplaceholder.typicode.com/posts/1 - POST a new post with a JSON body:
curl -X POST https://jsonplaceholder.typicode.com/posts \ -H "Content-Type: application/json" \ -d '{"title":"my post","body":"my content","userId":1}' - Observe the response body — JSONPlaceholder echoes your input back with a new ID.
Mini Challenge
Write a small JavaScript or Python script that calls the Open-Meteo API, parses the JSON body, and prints just the temperature value. Hint: the response body has a current object with a temperature_2m field.
Key Takeaways
- The body is the actual content of an HTTP message; headers describe it.
- Request body: required for POST/PUT/PATCH; forbidden (by convention) for GET.
- Response body: usually JSON for success and error; empty for 204 and 304.
- Always set
Content-Typewhen sending a body, and always check it when receiving one. - Parse the body with the right method for your language (
response.json()in JS,response.json()in Python).
Previously: Lesson 06 covered headers.
Today: You learned what goes inside the body — the actual data.
Next: In lesson 08 — URL Anatomy, you'll learn the structure of the URL itself, including paths and query parameters.
FAQ
Is there a size limit on HTTP request bodies?
No fixed limit in the HTTP spec, but servers and clients impose their own. Typical defaults: 1MB–10MB for general APIs, larger for file-upload endpoints. Large bodies should be sent with Transfer-Encoding: chunked instead of Content-Length.
Can a GET request have a body?
Technically yes, but the spec says servers may ignore it. Many proxies and caches strip it. Don't rely on it — use query parameters for filtering and the body for POST/PUT/PATCH only.
Comments
Comments
Post a Comment