Last Updated : 12 Jul, 2025
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 :
# 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
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 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 -
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 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 -
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 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 -
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 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.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 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 -
Check that b' at the start of output, it means the reference to a bytes object.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4