Response Methods - Python requests - GeeksforGeeks

23 Jul, 2021
When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being – get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of code. For example, response.status_code returns the status code from the headers itself, and one can check if the request was processed successfully or not. Response object can be used to imply lots of features, methods, and functionalities. Example :  
Python3
# import requests moduleimport requests # Making a get requestresponse = requests.get('https://api.github.com/') # print request objectprint(response.url) # print status codeprint(response.status_code)
Save this file as request.py, and run using below command  
Python request.py
 
Status code 200 indicates that request was made successfully.  
 
 
 
Some methods are most commonly used with response, such as response.json(), response.status_code, response.ok, etc. Requests is mostly used for making http request to APIs(Application Programming Interface). Some of commonly used response methods are discussed here –  
response.json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error).  
To illustrate use of response.json(), let’s ping geeksforgeeks.org. To run this script, you need to have Python and requests installed on your PC. 
Example code –
 
Python3
# import requests moduleimport requests # Making a get requestresponse = requests.get('https://api.github.com') # print responseprint(response) # print json contentprint(response.json())
Example Implementation –
Save above file as request.py and run using  
Python request.py
 
Output –
 
Check the json content at the terminal output. This basically returns a Python dictionary. 
response.ok returns True if status_code is less than 200, otherwise False. 
To illustrate use of response.ok, let’s ping geeksforgeeks.org. To run this script, you need to have Python and requests installed on your PC. 
Example code –
 
Python3
# import requests moduleimport requests # Making a get requestresponse = requests.get('https://api.github.com/') # print responseprint(response) # print if status code is less than 200print(response.ok)
Example Implementation –
Save above file as request.py and run using  
Python request.py
 Output –
Check that True which matches the condition of request being less than or equal to 200.  
response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found).  
To illustrate use of response.status_code, let’s ping api.github.com. To run this script, you need to have Python and requests installed on your PC. 
Example code –
Python3
# import requests moduleimport requests # Making a get requestresponse = requests.get('https://api.github.com/') # print responseprint(response) # print request status_codeprint(response.status_code)
Example Implementation –
Save above file as request.py and run using  
Python request.py
Output –
Check that and 200 in the output which refer to HttpResponse and Status code respectively.  
response.headers returns a dictionary of response headers. To check more about headers, visit – Different HTTP Headers 
To illustrate use of response.headers, let’s ping API of Github. To run this script, you need to have Python and requests installed on your PC. 
Example code –
 
Python3
# import requests moduleimport requests # Making a get requestresponse = requests.get('https://api.github.com') # print responseprint(response) # print headers of responseprint(response.headers)
Example Implementation –
Save above file as request.py and run using  
Python request.py
 Output – 
response.content returns the content of the response, in bytes. Basically, it refers to Binary Response content. 
To illustrate use of response.content, let’s ping API of Github. To run this script, you need to have Python and requests installed on your PC. 
Example code –
Python3
import requests # Making a get requestresponse = requests.get('https://api.github.com') # printing request contentprint(response.content)
Example Implementation –
Save above file as request.py and run using  
Python request.py
Output – 
Check that b’ at the start of output, it means the reference to a bytes object. 
kapoorsagar226
Python-requests
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
sum() function in Python
How To Convert Python Dictionary To JSON?