0
0
Rest APIprogramming~10 mins

First API request and response 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 make a GET request to the API endpoint.

Rest API
response = requests.[1]('https://api.example.com/data')
Drag options to blanks, or click blank then click option'
Aget
Bput
Cdelete
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' when only retrieving data.
Using 'put' or 'delete' which are for updating or deleting data.
2fill in blank
medium

Complete the code to print the status code of the API response.

Rest API
print(response.[1])
Drag options to blanks, or click blank then click option'
Acontent
Btext
Cjson
Dstatus_code
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' or 'content' which contain the response body, not the status code.
Using 'json' which parses the response body, not the status code.
3fill in blank
hard

Fix the error in the code to parse the JSON response correctly.

Rest API
data = response.[1]()
Drag options to blanks, or click blank then click option'
Atext
Bcontent
Cjson
Dstatus_code
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' or 'content' which return raw strings or bytes.
Using 'status_code' which is an integer, not the response body.
4fill in blank
hard

Fill both blanks to check if the response status code is 200 and print success.

Rest API
if response.[1] == [2]:
    print('Success!')
Drag options to blanks, or click blank then click option'
Astatus_code
Btext
C200
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing the response text to '200' instead of the status code.
Using 'json' or 'text' instead of 'status_code' for the check.
5fill in blank
hard

Fill both blanks to extract the 'name' field from the JSON response and print it.

Rest API
data = response.[1]()
name = data[[2]]
print(name)
Drag options to blanks, or click blank then click option'
Ajson
B'name'
C'id'
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'json' to parse the response.
Accessing the wrong key like 'id' instead of 'name'.