📖 Scenario: You are building a simple REST API that accepts user information in the request body. This is common when users submit forms on websites or apps.
🎯 Goal: Learn how to create and use the request body structure to send data to a REST API endpoint.
📋 What You'll Learn
Create a JSON object representing user data
Add a configuration variable for content type
Write code to send the JSON data in the request body
Print the response status to confirm the request
💡 Why This Matters
🌍 Real World
APIs often require sending data in the request body to create or update resources, like user profiles or orders.
💼 Career
Understanding request body structure is essential for backend and frontend developers working with REST APIs in web and mobile apps.
Progress0 / 4 steps
1
Create the user data JSON object
Create a JSON object called user_data with these exact key-value pairs: "name": "John Doe", "email": "john@example.com", and "age": 30.
Rest API
Hint
Use curly braces {} to create a JSON object in your code.
2
Set the content type header
Create a variable called headers and set it to a dictionary with the key "Content-Type" and value "application/json".
Rest API
Hint
Headers tell the server what kind of data you are sending.
3
Send the POST request with JSON body
Use the requests.post method to send a POST request to "https://example.com/api/users" with json=user_data and headers=headers. Store the response in a variable called response.
Rest API
Hint
Use the json= parameter to send JSON data in the request body.
4
Print the response status code
Write a print statement to display response.status_code to confirm the request was sent.
Rest API
Hint
The status code tells you if the request was successful (like 200 or 201) or if there was an error.
Practice
(1/5)
1. What is the main purpose of the request body in a REST API POST request?
easy
A. To define the HTTP method used
B. To send data from the client to the server
C. To specify the URL of the API endpoint
D. To set the server response status code
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 B
Quick Check:
Request body = data sent to server [OK]
Hint: Request body carries data sent to server in POST/PUT [OK]
Common Mistakes:
Confusing request body with URL or headers
Thinking request body sets HTTP method
Mixing request body with response status
2. Which of the following is the correct way to indicate JSON data in the request body header?
easy
A. Content-Type: text/plain
B. Content-Type: application/xml
C. Content-Type: application/json
D. Content-Type: multipart/form-data
Solution
Step 1: Identify the header for JSON data
The header Content-Type: application/json tells the server the body contains JSON formatted data.
Step 2: Compare with other content types
text/plain is plain text, multipart/form-data is for file uploads, and application/xml is for XML data.
Final Answer:
Content-Type: application/json -> Option C
Quick Check:
JSON header = application/json [OK]
Hint: Use application/json for JSON request bodies [OK]
Common Mistakes:
Using text/plain for JSON data
Forgetting to set Content-Type header
Using multipart/form-data without files
3. Given this JSON request body sent to an API:
{
"name": "Alice",
"age": 30
}
What will the server receive as the value of age?
medium
A. Undefined
B. "30" (string)
C. null
D. 30 (number)
Solution
Step 1: Analyze the JSON data types
The value for age is written as 30 without quotes, so it is a number, not a string.
Step 2: Understand server parsing
The server parses JSON and keeps numbers as numbers, so age will be the number 30.
Final Answer:
30 (number) -> Option D
Quick Check:
Unquoted numbers in JSON = number type [OK]
Hint: Numbers in JSON have no quotes, so parsed as numbers [OK]
Common Mistakes:
Assuming all JSON values are strings
Confusing number and string types
Ignoring JSON parsing rules
4. You send this request body:
{
"username": "bob",
"password": "1234"
}
But the server returns a 400 Bad Request error. What is a likely cause?
medium
A. Missing Content-Type header or wrong format
B. The username is too short
C. The password is numeric
D. The request method is GET
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 A
Quick Check:
400 error often = bad or missing Content-Type [OK]
Hint: Always set correct Content-Type header to avoid 400 errors [OK]
Common Mistakes:
Ignoring Content-Type header
Assuming 400 means validation error
Confusing GET with POST request body
5. You want to send a nested JSON object in the request body to create a user with address info:
Which Content-Type header and body format should you use to ensure the server correctly understands this nested data?
hard
A. Content-Type: application/json with the JSON body as shown
B. Content-Type: text/plain with JSON body as string
C. Content-Type: application/x-www-form-urlencoded with nested keys
D. Content-Type: multipart/form-data with JSON inside a file
Solution
Step 1: Identify correct header for nested JSON
Nested JSON requires application/json Content-Type so the server parses the structure correctly.
Step 2: Evaluate other formats
text/plain treats body as plain text, application/x-www-form-urlencoded does not support nested objects well, and multipart/form-data is for files, not nested JSON.
Final Answer:
Content-Type: application/json with the JSON body as shown -> Option A
Quick Check:
Nested JSON needs application/json header [OK]
Hint: Use application/json header for nested JSON objects [OK]
Common Mistakes:
Using form-urlencoded for nested JSON
Sending JSON as plain text without header
Uploading JSON as file instead of body
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.