The request body holds the data you send to a server when you want to create or update something. It helps the server understand what you want to do.
Request body structure in Rest API
POST /api/resource HTTP/1.1 Host: example.com Content-Type: application/json { "key1": "value1", "key2": "value2" }
The request body comes after the headers, separated by a blank line.
Content-Type header tells the server the format of the data sent (like JSON).
{
"username": "johndoe",
"password": "12345"
}{
"product": "Book",
"price": 9.99,
"inStock": true
}<form action="/submit" method="POST"> <input name="name" value="Alice" /> <input name="age" value="30" /> </form>
This Python program sends a POST request with a JSON body to a test API. It prints the status code and the response data from the server.
import requests url = "https://jsonplaceholder.typicode.com/posts" # Data to send in the request body body = { "title": "foo", "body": "bar", "userId": 1 } # Send POST request with JSON body response = requests.post(url, json=body) # Print response status and returned data print(f"Status code: {response.status_code}") print("Response body:") print(response.json())
Always set the correct Content-Type header to match your request body format.
Request body is usually used with POST, PUT, or PATCH HTTP methods.
GET requests typically do not have a request body.
The request body carries data sent to the server in POST or PUT requests.
It must be formatted correctly, often as JSON or form data.
Setting the right Content-Type header helps the server understand your data.
Practice
request body in a REST API POST request?Solution
Step 1: Understand the role of request body
The request body contains the data sent by the client to the server, usually in POST or PUT requests.Step 2: Differentiate from other parts of the request
The URL specifies the endpoint, the method defines the action, and the status code is part of the response, not the request body.Final Answer:
To send data from the client to the server -> Option BQuick Check:
Request body = data sent to server [OK]
- Confusing request body with URL or headers
- Thinking request body sets HTTP method
- Mixing request body with response status
Solution
Step 1: Identify the header for JSON data
The headerContent-Type: application/jsontells the server the body contains JSON formatted data.Step 2: Compare with other content types
text/plainis plain text,multipart/form-datais for file uploads, andapplication/xmlis for XML data.Final Answer:
Content-Type: application/json -> Option CQuick Check:
JSON header = application/json [OK]
- Using text/plain for JSON data
- Forgetting to set Content-Type header
- Using multipart/form-data without files
{
"name": "Alice",
"age": 30
}What will the server receive as the value of
age?Solution
Step 1: Analyze the JSON data types
The value forageis written as30without quotes, so it is a number, not a string.Step 2: Understand server parsing
The server parses JSON and keeps numbers as numbers, soagewill be the number 30.Final Answer:
30 (number) -> Option DQuick Check:
Unquoted numbers in JSON = number type [OK]
- Assuming all JSON values are strings
- Confusing number and string types
- Ignoring JSON parsing rules
{
"username": "bob",
"password": "1234"
}But the server returns a 400 Bad Request error. What is a likely cause?
Solution
Step 1: Check request headers and format
A 400 Bad Request often means the server cannot parse the body, commonly due to missing or incorrect Content-Type header.Step 2: Evaluate other options
Username length or password content usually cause validation errors, not 400 errors. GET requests do not have bodies, but the question implies a body was sent.Final Answer:
Missing Content-Type header or wrong format -> Option AQuick Check:
400 error often = bad or missing Content-Type [OK]
- Ignoring Content-Type header
- Assuming 400 means validation error
- Confusing GET with POST request body
{
"name": "Eve",
"address": {
"street": "123 Main St",
"city": "Townsville"
}
}Which Content-Type header and body format should you use to ensure the server correctly understands this nested data?
Solution
Step 1: Identify correct header for nested JSON
Nested JSON requiresapplication/jsonContent-Type so the server parses the structure correctly.Step 2: Evaluate other formats
text/plaintreats body as plain text,application/x-www-form-urlencodeddoes not support nested objects well, andmultipart/form-datais for files, not nested JSON.Final Answer:
Content-Type: application/json with the JSON body as shown -> Option AQuick Check:
Nested JSON needs application/json header [OK]
- Using form-urlencoded for nested JSON
- Sending JSON as plain text without header
- Uploading JSON as file instead of body
Ready to prove it?
Pass the test and this pattern moves unknown
