Complete the code to set the response content type to JSON.
response.headers['Content-Type'] = '[1]'
The Content-Type header tells the client the format of the response. For JSON, it should be application/json.
Complete the code to check if the client accepts JSON responses.
if 'application/json' in request.headers.get('[1]', ''):
The Accept header tells the server what content types the client can handle. Checking it for 'application/json' helps decide the response format.
Fix the error in the code to correctly parse the client's preferred content type.
preferred = request.headers.get('Accept', '').split(',')[[1]].strip()
The first item in the 'Accept' header list is at index 0. Using 0 gets the primary preferred content type.
Fill both blanks to create a dictionary comprehension that maps content types to their quality values from the Accept header.
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] ''}The first blank checks if the split has a quality value part; if not, it uses an empty string. The second blank filters out empty content types.
Fill all three blanks to return the best matching content type from the server's supported types based on client's Accept header.
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
The first blank handles quality parsing, the second filters out empty types, and the third provides a default quality of 0 for unsupported types.