Open In App

Response Methods – Python requests

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 module
import requests
 
# Making a get request
response = requests.get('https://api.github.com/')
 
# print request object
print(response.url)
 
# print status code
print(response.status_code)


Save this file as request.py, and run using below command 
 

Python request.py

 

response-python-requests

Status code 200 indicates that request was made successfully. 
 

Response Methods

 

 

Method Description
response.headers response.headers returns a dictionary of response headers.
response.encoding response.encoding returns the encoding used to decode response.content.
response.elapsed response.elapsed returns a timedelta object with the time elapsed from sending the request to the arrival of the response.
response.close() response.close() closes the connection to the server.
response.content response.content returns the content of the response, in bytes.
response.cookies response.cookies returns a CookieJar object with the cookies sent back from the server.
response.history response.history returns a list of response objects holding the history of request (url).
response.is_permanent_redirect response.is_permanent_redirect returns True if the response is the permanent redirected url, otherwise False.
response.is_redirect response.is_redirect returns True if the response was redirected, otherwise False.
response.iter_content() response.iter_content() iterates over the response.content.
response.json() response.json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error).
response.url response.url returns the URL of the response.
response.text response.text returns the content of the response, in unicode.
response.status_code response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found).
response.request response.request returns the request object that requested this response.
response.reason response.reason returns a text corresponding to the status code.
response.raise_for_status() response.raise_for_status() returns an HTTPError object if an error has occurred during the process.
response.ok response.ok returns True if status_code is less than 200, otherwise False.
response.links response.links returns the header links.

 

Commonly Used Response Methods

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()

response.json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). 
 

How to use response.json() using Python requests?

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 module
import requests
 
# Making a get request
response = requests.get('https://api.github.com')
 
# print response
print(response)
 
# print json content
print(response.json())


Example Implementation –

Save above file as request.py and run using 
 

Python request.py

 

Output –

 

response.json-Python-requests

Check the json content at the terminal output. This basically returns a Python dictionary.
 

response.ok

response.ok returns True if status_code is less than 200, otherwise False.
 

How to use response.ok using Python requests?

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 module
import requests
 
# Making a get request
response = requests.get('https://api.github.com/')
 
# print response
print(response)
 
# print if status code is less than 200
print(response.ok)


Example Implementation –

Save above file as request.py and run using 
 

Python request.py

 Output –

response.ok-Python-requests

Check that True which matches the condition of request being less than or equal to 200. 
 

response.status_code

response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). 
 

How to use response.status_code using Python requests?

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 module
import requests
 
# Making a get request
response = requests.get('https://api.github.com/')
 
# print response
print(response)
 
# print request status_code
print(response.status_code)


Example Implementation –

Save above file as request.py and run using 
 

Python request.py

Output –

response.status_code-Python-requests

Check that and 200 in the output which refer to HttpResponse and Status code respectively. 
 

response.headers

response.headers returns a dictionary of response headers. To check more about headers, visit – Different HTTP Headers
 

How to use response.headers using Python requests?

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 module
import requests
 
# Making a get request
response = requests.get('https://api.github.com')
 
# print response
print(response)
 
# print headers of response
print(response.headers)


Example Implementation –

Save above file as request.py and run using 
 

Python request.py

 Output –
 

response.headers-Python-requests

response.content

response.content returns the content of the response, in bytes. Basically, it refers to Binary Response content.
 

How to use response.content using Python requests?

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 request
response = requests.get('https://api.github.com')
 
# printing request content
print(response.content)


Example Implementation –

Save above file as request.py and run using 
 

Python request.py

Output –
 

response.content-Python-requests

Check that b’ at the start of output, it means the reference to a bytes object.
 



Last Updated : 23 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads