0
0
Rest APIprogramming~20 mins

First API request and response in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this simple GET request?
Consider this HTTP GET request to a public API that returns JSON data about a user. What will be the output printed by the code?
Rest API
import requests
response = requests.get('https://jsonplaceholder.typicode.com/users/1')
print(response.json()['username'])
A"Bret"
B"Leanne"
C"User1"
DKeyError
Attempts:
2 left
💡 Hint
Look at the user data returned by the API for user with ID 1.
🧠 Conceptual
intermediate
1:00remaining
Which HTTP status code indicates a successful GET request?
When you make a GET request to an API and it succeeds, which HTTP status code do you usually get?
A404
B500
C301
D200
Attempts:
2 left
💡 Hint
Think about the code meaning: 200 means OK.
Predict Output
advanced
2:00remaining
What is the output of this POST request code?
This code sends a POST request to create a new post. What will be printed?
Rest API
import requests
url = 'https://jsonplaceholder.typicode.com/posts'
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post(url, json=data)
print(response.json()['id'])
A1010
B101
C102
D201
Attempts:
2 left
💡 Hint
The API returns a new post with an id starting at 101.
🔧 Debug
advanced
2:00remaining
What error does this code raise?
This code tries to get a user but has a mistake. What error will it raise?
Rest API
import requests
response = requests.get('https://jsonplaceholder.typicode.com/users/9999')
print(response.json()['username'])
AKeyError
BIndexError
CTypeError
DNo error, prints username
Attempts:
2 left
💡 Hint
User 9999 does not exist, so the JSON response is empty.
🚀 Application
expert
2:00remaining
How many items are in the list returned by this API call?
This code fetches all users. How many user objects are in the returned list?
Rest API
import requests
response = requests.get('https://jsonplaceholder.typicode.com/users')
users = response.json()
print(len(users))
A5
B20
C10
D0
Attempts:
2 left
💡 Hint
The API returns 10 users in the list.