Consider this Python code using requests to get data from a server:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code)What will this print?
import requests response = requests.get('https://jsonplaceholder.typicode.com/posts/1') print(response.status_code)
Think about what status code means a successful GET request.
A status code of 200 means the server successfully found and returned the requested resource.
In a client-server model, which part handles the incoming requests and sends back responses?
Think about who listens and responds to requests.
The server processes requests from clients and sends back responses.
Look at this Python code snippet:
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/abc')
print(response.json()['id'])What error will occur when running this code?
Check what the server returns for an invalid post id and what keys exist in the JSON.
The server returns an empty object {} for invalid id, so accessing ['id'] causes a KeyError.
Which Python code snippet correctly sends JSON data to a REST API server?
Remember the parameter that automatically encodes JSON in requests.
The json parameter sends JSON-encoded data with correct headers.
A REST API server returns this JSON response for GET /users?active=true:
[{"id":1,"active":true},{"id":2,"active":true},{"id":3,"active":false},{"id":4,"active":true}]How many user objects will the client receive if the server filters correctly?
Count only users with active set to true.
There are 3 users with active":true in the list.