Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to send a POST request to create a new user.
Rest API
response = requests.[1]('https://api.example.com/users', json={'name': 'Alice'})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST will not create a resource.
Using DELETE or PUT are for other purposes.
✗ Incorrect
The post method sends data to create a new resource.
2fill in blank
mediumComplete the code to check if the POST request was successful.
Rest API
if response.status_code == [1]: print('User created successfully')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means OK but not specifically resource creation.
404 means not found, 500 means server error.
✗ Incorrect
Status code 201 means the resource was created successfully.
3fill in blank
hardFix the error in the code to send JSON data in the POST request.
Rest API
response = requests.post('https://api.example.com/users', [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a dictionary to
data sends form data, not JSON.Using
json.dumps with data sends a string, not JSON.✗ Incorrect
Use the json parameter to send JSON data properly.
4fill in blank
hardFill both blanks to create a POST request with headers and JSON data.
Rest API
headers = {'Content-Type': [1]
response = requests.post('https://api.example.com/users', headers=headers, [2]={'name': 'Bob'}) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
data instead of json sends form data.Setting Content-Type to
text/plain is incorrect for JSON.✗ Incorrect
Set the Content-Type header to application/json and use the json parameter to send JSON data.
5fill in blank
hardFill all three blanks to handle the POST response and print the new user's ID.
Rest API
response = requests.post('https://api.example.com/users', json={'name': 'Carol'}) if response.status_code == [1]: data = response.[2]() print('New user ID:', data[[3]])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 200 instead of 201.
Using
response.text instead of response.json().Accessing a wrong key instead of 'id'.
✗ Incorrect
Check for status code 201, parse JSON response with json(), and access the 'id' key.