0
0
Rest APIprogramming~10 mins

DELETE for removing resources in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a DELETE request to remove a user by ID.

Rest API
response = requests.[1]('https://api.example.com/users/123')
Drag options to blanks, or click blank then click option'
Apost
Bget
Cdelete
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of DELETE will only retrieve data, not remove it.
Using POST or PUT will not delete the resource.
2fill in blank
medium

Complete the code to check if the DELETE request was successful by verifying the status code.

Rest API
if response.status_code == [1]:
    print('Deleted successfully')
Drag options to blanks, or click blank then click option'
A204
B404
C200
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 200 instead of 204 might work but 204 is more common for DELETE success.
Checking for 404 or 500 means error states, not success.
3fill in blank
hard

Fix the error in the code to correctly send a DELETE request with headers.

Rest API
headers = {'Authorization': 'Bearer token123'}
response = requests.[1]('https://api.example.com/items/45', headers=headers)
Drag options to blanks, or click blank then click option'
Aput
Bpost
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or GET instead of DELETE will not remove the resource.
Forgetting to include headers when authorization is needed.
4fill in blank
hard

Fill both blanks to create a function that deletes a resource by ID and returns True if successful.

Rest API
def delete_resource(resource_id):
    response = requests.[1](f'https://api.example.com/resources/{resource_id}')
    return response.status_code [2] 204
Drag options to blanks, or click blank then click option'
Adelete
B==
C!=
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of DELETE.
Checking status code with != instead of ==.
5fill in blank
hard

Fill all three blanks to delete a user with authorization and handle errors properly.

Rest API
def delete_user(user_id, token):
    headers = {'Authorization': f'Bearer [1]'}
    response = requests.[2](f'https://api.example.com/users/{user_id}', headers=headers)
    if response.status_code == [3]:
        return True
    else:
        return False
Drag options to blanks, or click blank then click option'
Atoken
Bdelete
C204
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of DELETE.
Not passing the token correctly in headers.
Checking for wrong status code.