Header-based versioning helps servers know which version of an API a client wants by checking special information in the request headers. This keeps URLs clean and lets clients choose versions easily.
Header-based versioning in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
GET /resource HTTP/1.1
Host: api.example.com
Accept: application/vnd.example.v1+jsonThe version is specified in the Accept header or a custom header like X-API-Version.
The server reads this header to decide which version of the API to send back.
Accept header.GET /users HTTP/1.1
Host: api.example.com
Accept: application/vnd.example.v1+jsonX-API-Version to ask for version 2.GET /users HTTP/1.1 Host: api.example.com X-API-Version: 2
This simple Flask app shows how to read a custom header X-API-Version to return different data formats for different API versions.
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/users') def users(): version = request.headers.get('X-API-Version', '1') if version == '1': return jsonify({'users': ['Alice', 'Bob']}) elif version == '2': return jsonify({'users': [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]}) else: return jsonify({'error': 'Unsupported API version'}), 400 if __name__ == '__main__': app.run(debug=True)
Clients must include the correct header to get the right API version.
Servers should handle missing or unknown versions gracefully.
Header-based versioning keeps URLs clean but requires clients to set headers properly.
Header-based versioning uses HTTP headers to specify API versions.
This method keeps URLs simple and separates version info from resource paths.
Servers read headers like Accept or custom headers to decide response format.
Practice
Solution
Step 1: Understand header-based versioning concept
Header-based versioning uses HTTP headers to indicate which API version the client wants.Step 2: Compare with other versioning methods
Unlike URL or query parameter versioning, header-based keeps URLs clean and uses headers instead.Final Answer:
To specify the API version using HTTP headers instead of URLs -> Option BQuick Check:
Header versioning = version in HTTP headers [OK]
- Confusing header versioning with URL versioning
- Thinking version is in request body
- Assuming query parameters are used
Solution
Step 1: Identify common headers for versioning
TheAcceptheader is often used to specify the desired API version by content negotiation.Step 2: Exclude unrelated headers
Content-Typespecifies data format sent,Authorizationis for credentials, andUser-Agentidentifies client software.Final Answer:
Accept -> Option CQuick Check:
Version in Accept header = D [OK]
- Using Content-Type instead of Accept for versioning
- Confusing Authorization with version header
- Thinking User-Agent controls version
Accept: application/vnd.example.v2+jsonWhich API version is the client requesting?
Solution
Step 1: Parse the Accept header value
The valueapplication/vnd.example.v2+jsonincludesv2, indicating version 2.Step 2: Confirm version number meaning
Thev2part is a common pattern to specify version 2 of the API.Final Answer:
Version 2 -> Option AQuick Check:
v2 in Accept header means version 2 [OK]
- Ignoring the 'v2' part and guessing version 1
- Assuming no version if not in URL
- Confusing +json suffix with version
version = request.headers.get('Content-Type')
if version == 'application/vnd.example.v1+json':
return 'Version 1'
else:
return 'Unknown version'What is the main issue here?
Solution
Step 1: Identify header used for versioning
Header-based versioning typically uses theAcceptheader, notContent-Type.Step 2: Explain why Content-Type is wrong here
Content-Typedescribes the data format sent by the client, not the requested API version.Final Answer:
Using Content-Type header instead of Accept header for versioning -> Option DQuick Check:
Version header should be Accept, not Content-Type [OK]
- Checking Content-Type for version info
- Assuming request.headers.get is invalid
- Ignoring else branch
Solution
Step 1: Understand header-based versioning goal
It requires checking the HTTP headers to determine which API version to serve.Step 2: Identify correct version detection method
Checking theAcceptheader for specific version strings and routing requests accordingly is the right approach.Final Answer:
Check the Accept header for 'application/vnd.example.v1+json' or 'application/vnd.example.v2+json' and route accordingly -> Option AQuick Check:
Route by Accept header version strings [OK]
- Mixing header versioning with URL or query parameter versioning
- Ignoring version headers and serving one version only
- Using wrong headers for version detection
