Bird
Raised Fist0
Rest APIprogramming~10 mins

Content negotiation in Rest API - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the response content type to JSON.

Rest API
response.headers['Content-Type'] = '[1]'
Drag options to blanks, or click blank then click option'
Atext/html
Btext/plain
Capplication/json
Dapplication/xml
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/html' instead of 'application/json' for JSON responses.
Forgetting to set the Content-Type header.
2fill in blank
medium

Complete the code to check if the client accepts JSON responses.

Rest API
if 'application/json' in request.headers.get('[1]', ''):
Drag options to blanks, or click blank then click option'
AContent-Type
BAccept
CAuthorization
DUser-Agent
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'Content-Type' header instead of 'Accept'.
Not handling the case when the header is missing.
3fill in blank
hard

Fix the error in the code to correctly parse the client's preferred content type.

Rest API
preferred = request.headers.get('Accept', '').split(',')[[1]].strip()
Drag options to blanks, or click blank then click option'
A2
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0, causing an error if only one type is present.
Using negative index which may not be intended here.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps content types to their quality values from the Accept header.

Rest API
qualities = {ctype: float(q.split('=')[1]) if 'q=' in q else 1.0 for ctype, q in (item.split(';')[1] for item in request.headers.get('Accept', '').split(',')) if ctype [2] ''}
Drag options to blanks, or click blank then click option'
A if len(item.split(';')) > 1 else ''
B!=
C==
D else None
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the filter condition.
Not handling items without quality values properly.
5fill in blank
hard

Fill all three blanks to return the best matching content type from the server's supported types based on client's Accept header.

Rest API
def best_match(supported_types):
    qualities = {ctype: float(q.split('=')[1]) if 'q=' in q else 1.0 for ctype, q in (item.split(';')[1] for item in request.headers.get('Accept', '').split(',')) if ctype [2] ''}
    best = max(supported_types, key=lambda t: qualities.get(t, [3]))
    return best
Drag options to blanks, or click blank then click option'
A if len(item.split(';')) > 1 else ''
B!=
C0
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the filter.
Using None or other values instead of 0 as default quality.

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

  1. Step 1: Understand content negotiation role

    Content negotiation lets clients tell servers what data format they prefer.
  2. Step 2: Identify how clients specify format

    Clients use the Accept header to communicate their preferred format.
  3. Final Answer:

    To allow clients to specify the data format they want using the Accept header -> Option A
  4. 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

  1. Step 1: Recall MIME types for common formats

    JSON format uses the MIME type 'application/json'.
  2. Step 2: Match Accept header to JSON

    The correct Accept header for JSON is 'Accept: application/json'.
  3. Final Answer:

    Accept: application/json -> Option D
  4. 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

  1. Step 1: Understand quality values (q) in Accept header

    The q value shows preference; higher q means higher priority.
  2. 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.
  3. Final Answer:

    application/xml -> Option A
  4. 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

  1. Step 1: Understand HTTP 406 status meaning

    406 means server cannot provide a response matching the Accept header.
  2. Step 2: Link 406 to content negotiation failure

    If client requests unsupported format, server responds 406 Not Acceptable.
  3. Final Answer:

    Client requested a format the server cannot provide -> Option B
  4. 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

  1. Step 1: Understand content negotiation best practice

    The server should read the Accept header to decide response format.
  2. Step 2: Choose approach matching client preference

    Responding with JSON or XML based on Accept header is correct behavior.
  3. Final Answer:

    Check the Accept header and respond with JSON or XML accordingly -> Option C
  4. Quick Check:

    Content negotiation = respond per Accept header [OK]
Hint: Always respect Accept header to choose response format [OK]
Common Mistakes:
  • Ignoring Accept header and hardcoding response
  • Sending multiple formats in one response
  • Using unrelated custom headers for format choice
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