Bird
Raised Fist0
Rest APIprogramming~10 mins

Content negotiation in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Content negotiation
Client sends HTTP request with Accept header
↓
Server reads Accept header
↓
Server checks supported content types
↓
Match found?
No→Send 406 Not Acceptable
Yes↓
Server sends response in matched content type
The client requests a specific content type via the Accept header; the server checks if it supports it and responds accordingly or sends an error.
Execution Sample
Rest API
GET /data HTTP/1.1
Host: example.com
Accept: application/json

--- Server checks Accept header ---
If 'application/json' supported:
  Respond with JSON data
Else:
  Respond 406 Not Acceptable
This example shows a client requesting JSON data and the server responding with JSON if supported.
Execution Table
StepActionAccept HeaderServer Supported TypesMatch Found?Server Response
1Client sends requestapplication/jsonapplication/json, text/htmlYesWaiting for server processing
2Server reads Accept headerapplication/jsonapplication/json, text/htmlYesChecking supported types
3Server finds matchapplication/jsonapplication/json, text/htmlYesPrepare JSON response
4Server sends responseapplication/jsonapplication/json, text/htmlYes200 OK with JSON data
5Client sends requestapplication/xmlapplication/json, text/htmlNoWaiting for server processing
6Server reads Accept headerapplication/xmlapplication/json, text/htmlNoChecking supported types
7Server finds no matchapplication/xmlapplication/json, text/htmlNoSend 406 Not Acceptable
💡 Execution stops after server sends response or 406 error based on content negotiation
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5After Step 7
Accept HeaderNoneapplication/jsonapplication/jsonapplication/jsonapplication/xmlapplication/xml
Server Supported Typesapplication/json, text/htmlapplication/json, text/htmlapplication/json, text/htmlapplication/json, text/htmlapplication/json, text/htmlapplication/json, text/html
Match Found?NoYesYesYesNoNo
Server ResponseNoneWaiting for server processingPrepare JSON response200 OK with JSON dataWaiting for server processing406 Not Acceptable
Key Moments - 3 Insights
Why does the server send a 406 Not Acceptable response?
Because the Accept header requests a content type the server does not support, as shown in execution_table rows 5-7 where 'application/xml' is not matched.
What happens if the Accept header is missing?
The server usually sends a default content type response, but this example assumes Accept header is present; without it, server picks a default supported type.
Can the Accept header list multiple types?
Yes, the Accept header can list multiple types with priorities; the server picks the best match from its supported types.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server response at step 4?
A200 OK with JSON data
B406 Not Acceptable
CWaiting for server processing
DPrepare XML response
💡 Hint
Check the 'Server Response' column at step 4 in execution_table
At which step does the server determine no matching content type is found?
AStep 3
BStep 6
CStep 7
DStep 2
💡 Hint
Look for 'Match Found?' = No and 'Server Response' = 'Send 406 Not Acceptable' in execution_table
If the client changes Accept header to 'text/html', what would be the server response?
A406 Not Acceptable
B200 OK with HTML data
C200 OK with JSON data
DWaiting for server processing
💡 Hint
Server supports 'text/html' as per 'Server Supported Types' in variable_tracker
Concept Snapshot
Content negotiation lets clients tell servers what data format they want using the Accept header.
Server checks if it supports the requested type.
If yes, server sends data in that format.
If no, server sends 406 Not Acceptable error.
This helps clients get data they can understand.
Full Transcript
Content negotiation is a process where a client sends an HTTP request with an Accept header specifying the desired content type, such as application/json. The server reads this header and compares it to the content types it supports. If the server supports the requested type, it responds with data in that format and a 200 OK status. If not, it responds with a 406 Not Acceptable error. This process ensures clients receive data in a format they can handle. The execution table shows steps where the server reads the Accept header, checks for matches, and sends the appropriate response. Variables like Accept Header, Server Supported Types, Match Found, and Server Response change during these steps. Common confusions include why a 406 error is sent and what happens if the Accept header is missing or lists multiple types. The visual quiz tests understanding of these steps and responses.

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