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?
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'
Check the order of if and elif conditions and how substring matching works.
The code checks if the string 'application/json' is anywhere in the accept_header. Since it is present (after the comma), the first if condition is true, so response is set to 'JSON response'. The elif is not checked after that.
In REST APIs, clients specify the desired response format using a specific HTTP header. Which header is this?
It tells the server what media types the client can handle.
The Accept header is used by clients to tell the server which content types they prefer. The server uses this to perform content negotiation and respond accordingly.
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')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')
Check the order of conditions and presence of substrings in the header string.
The string 'application/json' is not present, so first if fails. The string 'application/xml' is present (with quality factor), so the second elif is true and prints 'Send XML'. The later conditions are not checked.
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'?
accept_header = 'application/xml' if accept_header == 'application/json' or 'application/xml': response = 'JSON response' else: response = 'Default response'
Look carefully at how the or operator is used in the condition.
The condition accept_header == 'application/json' or 'application/xml' is evaluated as (accept_header == 'application/json') or ('application/xml'). Since non-empty strings are always true in Python, the condition always evaluates to true, so response is always 'JSON response'.
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?
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))
Count how many unique media types are extracted from the header string.
The dictionary comprehension splits the header by commas, then for each part extracts the media type before any semicolon and assigns a quality factor (default 1.0 if missing). The header has three media types: 'text/html', 'application/json', and 'application/xml'. So the dictionary has 3 keys, and len(media_types) is 3.