0
0
Rest APIprogramming~20 mins

Content negotiation in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Content Negotiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this HTTP Accept header handling code?

Consider a REST API server that supports JSON and XML responses. The server code snippet below processes the Accept header to decide the response format.

accept_header = 'application/xml,application/json;q=0.9'

if 'application/json' in accept_header:
    response = 'JSON response'
elif 'application/xml' in accept_header:
    response = 'XML response'
else:
    response = 'Default response'

What will be the value of response after running this code?

Rest API
accept_header = 'application/xml,application/json;q=0.9'

if 'application/json' in accept_header:
    response = 'JSON response'
elif 'application/xml' in accept_header:
    response = 'XML response'
else:
    response = 'Default response'
AJSON response
BXML response
CDefault response
DError: header parsing failed
Attempts:
2 left
💡 Hint

Check the order of if and elif conditions and how substring matching works.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP header is primarily used for content negotiation?

In REST APIs, clients specify the desired response format using a specific HTTP header. Which header is this?

AUser-Agent
BAuthorization
CContent-Type
DAccept
Attempts:
2 left
💡 Hint

It tells the server what media types the client can handle.

Predict Output
advanced
2:00remaining
What is the output of this server content negotiation snippet?

Given the following Python code snippet simulating content negotiation, what will be printed?

accept_header = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

if 'application/json' in accept_header:
    print('Send JSON')
elif 'application/xml' in accept_header:
    print('Send XML')
elif 'text/html' in accept_header:
    print('Send HTML')
else:
    print('Send default')
Rest API
accept_header = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

if 'application/json' in accept_header:
    print('Send JSON')
elif 'application/xml' in accept_header:
    print('Send XML')
elif 'text/html' in accept_header:
    print('Send HTML')
else:
    print('Send default')
ASend XML
BSend HTML
CSend JSON
DSend default
Attempts:
2 left
💡 Hint

Check the order of conditions and presence of substrings in the header string.

🔧 Debug
advanced
2:30remaining
Why does this content negotiation code always return JSON?

Look at this Python code snippet:

accept_header = 'application/xml'

if accept_header == 'application/json' or 'application/xml':
    response = 'JSON response'
else:
    response = 'Default response'

Why does response always become 'JSON response' even when accept_header is 'application/xml'?

Rest API
accept_header = 'application/xml'

if accept_header == 'application/json' or 'application/xml':
    response = 'JSON response'
else:
    response = 'Default response'
ABecause accept_header is overwritten before the if statement
BBecause the code compares accept_header to both strings correctly
CBecause 'application/xml' is always truthy, so the condition is always true
DBecause the else block is never reached due to syntax error
Attempts:
2 left
💡 Hint

Look carefully at how the or operator is used in the condition.

🚀 Application
expert
3:00remaining
How many media types will this content negotiation dictionary contain?

Given this Python dictionary comprehension that parses an Accept header string into media types and their quality factors:

accept_header = 'text/html,application/json;q=0.8,application/xml;q=0.9'

media_types = {part.split(';')[0]: float(part.split('q=')[1]) if 'q=' in part else 1.0 for part in accept_header.split(',')}

print(len(media_types))

What is the output of the print statement?

Rest API
accept_header = 'text/html,application/json;q=0.8,application/xml;q=0.9'

media_types = {part.split(';')[0]: float(part.split('q=')[1]) if 'q=' in part else 1.0 for part in accept_header.split(',')}

print(len(media_types))
A2
B3
C1
DRaises ValueError
Attempts:
2 left
💡 Hint

Count how many unique media types are extracted from the header string.