Working with Request Headers: Content-Type and Accept
📖 Scenario: You are building a simple REST API client that sends data to a server and expects a response in a specific format. To do this correctly, you need to set the right request headers: Content-Type to tell the server what format you are sending, and Accept to tell the server what format you want back.
🎯 Goal: Learn how to set the Content-Type and Accept headers in an HTTP request to communicate properly with a REST API.
📋 What You'll Learn
Create a dictionary called headers with the exact keys and values specified.
Add a variable called url with the exact URL string.
Use the requests.post method with the headers dictionary.
Print the response status code and response content.
💡 Why This Matters
🌍 Real World
Setting the correct request headers is essential when working with APIs to ensure the server understands the data format you send and the format you want back.
💼 Career
Many jobs in software development, especially backend and frontend roles, require working with APIs and setting headers correctly to integrate different systems.
Progress0 / 4 steps
1
DATA SETUP: Create the URL variable
Create a variable called url and set it to the string "https://api.example.com/data".
Rest API
Hint
Use quotes around the URL string exactly as shown.
2
CONFIGURATION: Create the headers dictionary
Create a dictionary called headers with these exact entries: "Content-Type": "application/json" and "Accept": "application/json".
Rest API
Hint
Use curly braces to create the dictionary and include both keys exactly as shown.
3
CORE LOGIC: Send a POST request with headers
Import the requests module. Use requests.post to send a POST request to url with the headers dictionary. Store the response in a variable called response.
Rest API
Hint
Remember to import the requests module before using it.
4
OUTPUT: Print the response status and content
Print the response status code using print(response.status_code). Then print the response content as text using print(response.text).
Rest API
Hint
Use two print statements: one for response.status_code and one for response.text.
Practice
(1/5)
1. What does the Content-Type header specify in an HTTP request?
easy
A. The format of the data being sent to the server
B. The format of the data the client wants to receive
C. The server's IP address
D. The HTTP method used in the request
Solution
Step 1: Understand the role of Content-Type
The Content-Type header tells the server what format the request body data is in, such as JSON or XML.
Step 2: Differentiate from Accept header
The Accept header tells the server what response format the client wants, not the data sent.
Final Answer:
The format of the data being sent to the server -> Option A
Quick Check:
Content-Type = request data format [OK]
Hint: Content-Type = your data format sent [OK]
Common Mistakes:
Confusing Content-Type with Accept header
Thinking Content-Type is about response format
Ignoring the header completely
2. Which of the following is the correct way to set the Accept header to request JSON data in an HTTP request?
easy
A. Content-Type: application/json
B. Accept: text/html
C. Accept: application/json
D. Content-Type: text/plain
Solution
Step 1: Identify the Accept header usage
The Accept header tells the server what response format the client wants, such as JSON.
Step 2: Match the correct MIME type for JSON
The MIME type for JSON is application/json, so the header should be Accept: application/json.
Final Answer:
Accept: application/json -> Option C
Quick Check:
Accept header for JSON = application/json [OK]
Hint: Accept header = desired response format [OK]
Common Mistakes:
Using Content-Type instead of Accept for response format
What format will the server expect the request body to be, and what format will it try to respond with?
medium
A. Request body XML, response JSON
B. Request body XML, response XML
C. Request body JSON, response JSON
D. Request body JSON, response XML
Solution
Step 1: Analyze Content-Type header
The Content-Type is application/json, so the server expects JSON data in the request body.
Step 2: Analyze Accept header
The Accept header is application/xml, so the client wants the response in XML format.
Final Answer:
Request body JSON, response XML -> Option D
Quick Check:
Content-Type = JSON, Accept = XML [OK]
Hint: Content-Type = sent data, Accept = wanted response [OK]
Common Mistakes:
Mixing up Content-Type and Accept roles
Assuming response matches request format
Ignoring headers and guessing defaults
4. You wrote this HTTP request:
POST /submit HTTP/1.1
Host: example.com
Content-Type: application/json
Accept: application/json
name=John&age=30
Why might the server reject this request or fail to parse the data?
medium
A. Because the HTTP method POST is not allowed
B. Because the Content-Type says JSON but the body is URL-encoded form data
C. Because Accept header is incorrect for JSON
D. Because the Host header is missing
Solution
Step 1: Check Content-Type and body format
The Content-Type is application/json, but the body is name=John&age=30, which is URL-encoded form data, not JSON.
Step 2: Understand server parsing behavior
The server expects JSON data but receives form data, so it may reject or fail to parse the request.
Final Answer:
Because the Content-Type says JSON but the body is URL-encoded form data -> Option B
Quick Check:
Content-Type must match body format [OK]
Hint: Match Content-Type with actual body format [OK]
Common Mistakes:
Ignoring mismatch between Content-Type and body
Assuming Accept header affects request body
Thinking Host header is missing when it is present
5. You want to send JSON data to an API and receive a JSON response. Which headers should you set correctly in your HTTP request to ensure this communication?
hard
A. Content-Type: application/json and Accept: application/json
B. Content-Type: text/plain and Accept: application/json
C. Content-Type: application/json and Accept: text/html
D. Content-Type: application/xml and Accept: application/json
Solution
Step 1: Set Content-Type for request data
Since you are sending JSON data, set Content-Type: application/json so the server knows the format of the data you send.
Step 2: Set Accept for response format
To receive JSON response, set Accept: application/json to tell the server you want JSON back.
Final Answer:
Content-Type: application/json and Accept: application/json -> Option A
Quick Check:
Both headers set to application/json for JSON exchange [OK]
Hint: Both headers must match JSON for send and receive [OK]
Common Mistakes:
Setting Content-Type and Accept to different formats
Using text/plain or XML when JSON is needed
Forgetting to set Accept header
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.