📖 Scenario: You are building a simple REST API that returns user information. Different clients want the data in different formats, like JSON or XML.
🎯 Goal: Create a REST API endpoint that returns user data. Use content negotiation to send JSON if the client asks for 'application/json' and XML if the client asks for 'application/xml'.
📋 What You'll Learn
Create a dictionary called user with keys 'name' and 'age' and values 'Alice' and 30
Create a variable called accept_header with the value 'application/json' or 'application/xml'
Write an if statement to check if accept_header is 'application/json' or 'application/xml'
Print the user data as JSON if accept_header is 'application/json'
Print the user data as XML if accept_header is 'application/xml'
💡 Why This Matters
🌍 Real World
APIs often need to send data in different formats depending on what the client wants. Content negotiation helps decide the format automatically.
💼 Career
Understanding content negotiation is important for backend developers building APIs that work well with many clients and devices.
Progress0 / 4 steps
1
Create the user data dictionary
Create a dictionary called user with these exact entries: 'name': 'Alice' and 'age': 30
Rest API
Hint
Use curly braces to create a dictionary. The keys are strings 'name' and 'age'. The values are 'Alice' and 30.
2
Set the Accept header variable
Create a variable called accept_header and set it to the string 'application/json'
Rest API
Hint
Just assign the string 'application/json' to the variable accept_header.
3
Write the content negotiation logic
Write an if statement that checks if accept_header is 'application/json' or 'application/xml'
Rest API
Hint
Use if and elif to check the value of accept_header.
4
Print user data in the requested format
Inside the if block for 'application/json', print the user dictionary as JSON using json.dumps(user). Inside the elif block for 'application/xml', print the user dictionary as XML with tags <user>, <name>, and <age>. Import the json module at the top.
Rest API
Hint
Use json.dumps(user) to convert the dictionary to a JSON string. For XML, build a string with tags and values.
Practice
(1/5)
1. What is the main purpose of content negotiation in REST APIs?
easy
A. To allow clients to specify the data format they want using the Accept header
B. To encrypt data between client and server
C. To authenticate users before sending data
D. To compress data for faster transfer
Solution
Step 1: Understand content negotiation role
Content negotiation lets clients tell servers what data format they prefer.
Step 2: Identify how clients specify format
Clients use the Accept header to communicate their preferred format.
Final Answer:
To allow clients to specify the data format they want using the Accept header -> Option A
Quick Check:
Content negotiation = Accept header usage [OK]
Hint: Remember: Accept header tells server your preferred format [OK]
Common Mistakes:
Confusing content negotiation with authentication
Thinking it compresses data
Believing it encrypts data
2. Which of the following is the correct way to specify JSON format in the Accept header?
easy
A. Accept: application/xml
B. Accept: text/html
C. Accept: image/png
D. Accept: application/json
Solution
Step 1: Recall MIME types for common formats
JSON format uses the MIME type 'application/json'.
Step 2: Match Accept header to JSON
The correct Accept header for JSON is 'Accept: application/json'.
Final Answer:
Accept: application/json -> Option D
Quick Check:
JSON MIME type = application/json [OK]
Hint: JSON MIME type always is application/json in Accept header [OK]
Common Mistakes:
Using text/html for JSON
Confusing image/png with data formats
Using application/xml instead of JSON
3. Given this HTTP request header: Accept: application/xml, application/json;q=0.8 Which content type will the server most likely respond with?
medium
A. application/xml
B. application/pdf
C. text/html
D. application/json
Solution
Step 1: Understand quality values (q) in Accept header
The q value shows preference; higher q means higher priority.
Step 2: Compare q values for application/xml and application/json
application/xml has no q value, so defaults to 1. application/json has q=0.8, lower priority.
Final Answer:
application/xml -> Option A
Quick Check:
Higher q or default 1 wins = application/xml [OK]
Hint: No q means 1.0, highest priority wins [OK]
Common Mistakes:
Ignoring q values and picking lower priority
Choosing JSON because it appears second
Selecting unrelated content types
4. A server returns a 406 Not Acceptable error. What is the most likely cause related to content negotiation?
medium
A. Server failed to authenticate the client
B. Client requested a format the server cannot provide
C. Client sent an invalid HTTP method
D. Server ran out of memory
Solution
Step 1: Understand HTTP 406 status meaning
406 means server cannot provide a response matching the Accept header.
Step 2: Link 406 to content negotiation failure
If client requests unsupported format, server responds 406 Not Acceptable.
Final Answer:
Client requested a format the server cannot provide -> Option B
Quick Check:
406 = unsupported requested format [OK]
Hint: 406 means no matching format found for Accept header [OK]
Common Mistakes:
Confusing 406 with authentication errors
Thinking 406 means server error
Assuming 406 is about HTTP methods
5. You want your REST API to support both JSON and XML responses. Which server-side approach correctly implements content negotiation?
hard
A. Ignore the Accept header and always send JSON
B. Send both JSON and XML in the same response body
C. Check the Accept header and respond with JSON or XML accordingly
D. Respond with XML only if the client sends a custom header
Solution
Step 1: Understand content negotiation best practice
The server should read the Accept header to decide response format.