Bird
Raised Fist0
Rest APIprogramming~20 mins

Request headers (Content-Type, Accept) in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Header Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:00remaining
What is the Content-Type header value sent in this HTTP request?

Consider this HTTP POST request snippet:

POST /api/data HTTP/1.1
Host: example.com
Content-Type: application/json
Accept: application/json

{"name": "Alice", "age": 30}

What is the value of the Content-Type header?

Aapplication/json
Btext/html
Capplication/xml
Dmultipart/form-data
Attempts:
2 left
💡 Hint

The Content-Type header tells the server what format the request body is in.

❓ Predict Output
intermediate
1:00remaining
What response format does the client expect based on the Accept header?

Given this HTTP GET request header:

GET /api/users HTTP/1.1
Host: example.com
Accept: application/xml

What format does the client expect the server to respond with?

Aapplication/json
Btext/plain
Capplication/xml
Dtext/html
Attempts:
2 left
💡 Hint

The Accept header tells the server what content types the client can handle.

🔧 Debug
advanced
1:30remaining
Why does this POST request fail to send JSON data correctly?

Look at this HTTP POST request:

POST /submit HTTP/1.1
Host: example.com
Accept: application/json

{"username": "bob", "score": 42}

Why might the server reject this request?

AHost header is missing
BAccept header is wrong, should be text/html
CRequest body is not valid JSON
DMissing Content-Type header, so server doesn't know the body format
Attempts:
2 left
💡 Hint

Check if the server knows what format the body is in.

🧠 Conceptual
advanced
1:00remaining
What happens if the Accept header is set to '*/*'?

In an HTTP request, the client sets the header Accept: */*. What does this mean?

AThe client accepts any content type in the response
BThe client only accepts plain text responses
CThe client rejects all responses
DThe client only accepts JSON responses
Attempts:
2 left
💡 Hint

Think about what the wildcard '*' means in this context.

❓ Predict Output
expert
1:30remaining
What is the output of this Python code simulating headers?

Consider this Python code that simulates checking headers:

headers = {
    "Content-Type": "application/json",
    "Accept": "application/xml"
}

if headers.get("Content-Type") == "application/json" and headers.get("Accept") == "application/xml":
    result = "Send JSON, expect XML"
else:
    result = "Headers mismatch"

print(result)

What will be printed?

Rest API
headers = {
    "Content-Type": "application/json",
    "Accept": "application/xml"
}

if headers.get("Content-Type") == "application/json" and headers.get("Accept") == "application/xml":
    result = "Send JSON, expect XML"
else:
    result = "Headers mismatch"

print(result)
AHeaders mismatch
BSend JSON, expect XML
CKeyError
DNone
Attempts:
2 left
💡 Hint

Check the dictionary keys and values carefully.

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

  1. 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.
  2. Step 2: Differentiate from Accept header

    The Accept header tells the server what response format the client wants, not the data sent.
  3. Final Answer:

    The format of the data being sent to the server -> Option A
  4. 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

  1. Step 1: Identify the Accept header usage

    The Accept header tells the server what response format the client wants, such as JSON.
  2. 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.
  3. Final Answer:

    Accept: application/json -> Option C
  4. 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
  • Using wrong MIME type like text/html for JSON
  • Mixing up header names
3. Given this HTTP request snippet:
POST /api/data HTTP/1.1
Host: example.com
Content-Type: application/json
Accept: application/xml

{"name": "Alice"}

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

  1. Step 1: Analyze Content-Type header

    The Content-Type is application/json, so the server expects JSON data in the request body.
  2. Step 2: Analyze Accept header

    The Accept header is application/xml, so the client wants the response in XML format.
  3. Final Answer:

    Request body JSON, response XML -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Because the Content-Type says JSON but the body is URL-encoded form data -> Option B
  4. 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

  1. 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.
  2. Step 2: Set Accept for response format

    To receive JSON response, set Accept: application/json to tell the server you want JSON back.
  3. Final Answer:

    Content-Type: application/json and Accept: application/json -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard