0
0
Rest APIprogramming~10 mins

Media type versioning in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Media type versioning
Client sends HTTP request
Server reads Accept header
Parse media type and version
Match version to API handler
Process request with correct version
Send response with versioned media type
Client receives response
The client sends a request with a media type specifying the API version. The server reads this, selects the correct version handler, processes the request, and responds with the matching versioned media type.
Execution Sample
Rest API
GET /users HTTP/1.1
Accept: application/vnd.example.v2+json

Response:
Content-Type: application/vnd.example.v2+json
Body: {"users": [...] }
Client requests version 2 of the users API by specifying it in the Accept header; server responds with version 2 media type and data.
Execution Table
StepActionHeader/ValueServer DecisionResponse HeaderResponse Body
1Client sends requestAccept: application/vnd.example.v2+jsonReads Accept header, detects version 2
2Server selects handlerUses API version 2 handler
3Server processes requestFetches users data for v2
4Server sends responseContent-Type: application/vnd.example.v2+json{"users": [...] }
5Client receives responseProcesses data as version 2 format
💡 Request completed with version 2 media type; client and server agree on API version.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Accept HeaderNoneapplication/vnd.example.v2+jsonapplication/vnd.example.v2+jsonapplication/vnd.example.v2+jsonapplication/vnd.example.v2+json
API VersionNoneDetected v2v2v2v2
Response Content-TypeNoneNoneNoneSet to v2 media typeapplication/vnd.example.v2+json
Response BodyNoneNoneNoneSet with v2 users data{"users": [...] }
Key Moments - 2 Insights
Why does the server look at the Accept header instead of the URL to determine the API version?
Because media type versioning uses the Accept header to specify the version, allowing the URL to stay clean and stable. This is shown in execution_table row 1 where the server reads the Accept header to detect version 2.
What happens if the client sends an Accept header with a version the server does not support?
The server cannot find a matching handler and usually returns an error or default version. This is implied by the server decision step in execution_table row 2 where it selects the handler based on the version.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what version does the server detect from the Accept header at step 1?
Av2
Bv3
Cv1
DNo version detected
💡 Hint
Check the 'Header/Value' and 'Server Decision' columns in row 1 of execution_table.
At which step does the server set the response Content-Type header to the versioned media type?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Response Header' column in execution_table rows.
If the client changed the Accept header to 'application/vnd.example.v1+json', how would the API Version variable change in variable_tracker?
AIt would stay as v2
BIt would become None
CIt would change to v1
DIt would become v3
💡 Hint
Refer to the 'API Version' row in variable_tracker and how it matches Accept header values.
Concept Snapshot
Media type versioning uses the Accept header to specify API version.
Server reads Accept header to select correct API version handler.
Response Content-Type matches requested version.
Keeps URLs clean and supports multiple API versions.
Client and server agree on version via media type.
Example: Accept: application/vnd.example.v2+json
Full Transcript
Media type versioning is a way to manage different versions of an API by using the HTTP Accept header. The client sends a request with an Accept header that includes the version number in the media type, like application/vnd.example.v2+json. The server reads this header, detects the requested version, and selects the appropriate API handler for that version. It then processes the request and sends back a response with the Content-Type header set to the same versioned media type. This approach keeps the URL stable and clean while allowing multiple API versions to coexist. The execution table shows the step-by-step flow from client request to server response, tracking how the Accept header is read, the version is detected, and the response is formed. The variable tracker shows how key variables like Accept header, API version, response Content-Type, and response body change during the process. Common confusions include why the Accept header is used instead of the URL and what happens if an unsupported version is requested. The visual quiz tests understanding of these steps and variable changes.