0
0
Rest APIprogramming~10 mins

POST for creating 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 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'
Apost
Bget
Cdelete
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST will not create a resource.
Using DELETE or PUT are for other purposes.
2fill in blank
medium

Complete 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'
A500
B201
C404
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means OK but not specifically resource creation.
404 means not found, 500 means server error.
3fill in blank
hard

Fix 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'
A{'name': 'Alice'}
B'name=Alice'
Cjson={'name': 'Alice'}
Djson.dumps({'name': 'Alice'})
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.
4fill in blank
hard

Fill 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'
A'application/json'
Bjson
Cdata
D'text/plain'
Attempts:
3 left
💡 Hint
Common Mistakes
Using data instead of json sends form data.
Setting Content-Type to text/plain is incorrect for JSON.
5fill in blank
hard

Fill 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'
A201
Bjson
C'id'
D200
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'.