Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
API Basics APIs APIs From Zero to Real-World API Testing & Automation HTTP Module 1 — API Fundamentals

Request Body and Response Body — What Goes Inside HTTP Messages

Reviewed & accurate
AI Summary

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-Type header
  • 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

MethodBody required?
GETNo — should not have one
POSTUsually yes — the data to create
PUTYes — the full new state
PATCHYes — the fields to update
DELETEUsually 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-TypeBody formatUse case
application/jsonJSONThe default for modern APIs
application/x-www-form-urlencodedkey=value&key=valueHTML forms
multipart/form-dataBoundary-separated partsFile uploads
text/plainPlain textSimple text payloads
application/xmlXMLLegacy 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

  1. Forgetting Content-Type when sending a body. The server does not know how to parse the body without it.
  2. Sending a body with GET. Some servers reject it; many proxies strip it. Use query parameters instead.
  3. 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.
  4. Sending JSON with trailing commas or comments. JSON does not allow either. Use a validator — covered in lesson 14.
  5. 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)

  1. Use cURL to GET a post from JSONPlaceholder and look at the body:
    curl https://jsonplaceholder.typicode.com/posts/1
  2. 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}'
  3. 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-Type when 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).
Course continuity
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.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments