0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 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.