Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the Content-Type header to JSON.
Rest API
headers = {"Content-Type": "[1]"} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/html' or 'application/xml' instead of 'application/json'.
✗ Incorrect
The Content-Type header must be set to application/json to indicate JSON data.
2fill in blank
mediumComplete the code to convert a Python dictionary to a JSON string.
Rest API
import json json_data = json.[1](data)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
load or loads which are for reading JSON.✗ Incorrect
The json.dumps() function converts a Python object to a JSON string.
3fill in blank
hardFix the error in the code to parse JSON from a string.
Rest API
import json parsed = json.[1](json_string)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
load which reads from a file, not a string.✗ Incorrect
The json.loads() function parses a JSON string into a Python object.
4fill in blank
hardFill both blanks to create a JSON response with status code 200.
Rest API
response = [1](json.dumps(data), [2]=200, headers=headers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
jsonify which returns a response but does not accept status code like this.✗ Incorrect
make_response creates a response object. The status code is set with status.
5fill in blank
hardFill all three blanks to extract a value from JSON request data.
Rest API
from flask import request json_data = request.[1] value = json_data.get([2], [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
args which is for query parameters, not JSON body.✗ Incorrect
Use request.json to get JSON data. Use get("key", None) to safely access a value.