Manipulating requests Response
Lesson 7 of 10 in Coddy's API in Python course.
Once you either get() or post() into an API, you will receive a response object which we stored in the response variable in our last examples.
As we learned in the JSON lesson, these responses are in JSON format or in Python's native dictionary format.
To receive the JSON format of your response. You have to say,
data = response.json()You can then parse through the necessary values in the response, using the keying method of the dictionary.
Challenge
MediumInstructions:
- Send a GET request to:
https://jsonplaceholder.typicode.com/users/1 - Save the
requests.get()object to a variable calledresponse - Parse and save
response.json()into a variable calleddata - Use the keying method to save
data["name"]into a variable calledresult - Print
result- it should always display: Leanne Graham
Try it yourself
import requests
# Step 1: Send GET request
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Get the name
result = # Your code here
# Step 4: Print the result
print(result)All lessons in API in Python
2API in Python
requests Library for APIrequests Method to Retrieverequests Method to Send DataManipulating requests Response3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results