Python Requests Module Response Object
The Python Requests module is a popular library used for making HTTP requests in Python. It allows you to send HTTP/1.1 requests extremely easily. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries.
Response Object
After making a request using the Requests module, you will receive a response object. This object contains the server’s response to the request made. It has various properties and methods that allow you to access the data and information pertaining to the response.
To access the response object, you first have to make a request using the Requests module. Let’s assume we make a GET request:
import requests
response = requests.get('https://www.example.com')
Here, we made a GET request to "https://www.example.com" and stored the response in a variable called response. We can now access various properties of the response object:
response.status_code
- Returns the status code of the response (e.g. 200 for success, 404 for not found).response.content
- Returns the content of the response in bytes format.response.text
- Returns the content of the response in Unicode format.response.headers
- Returns the headers of the response.response.json()
- Returns the content of the response in JSON format.
Let’s print out some of these properties:
import requests
response = requests.get('https://www.example.com')
print(response.status_code)
print(response.content)
print(response.text)
print(response.headers)
The above code will output something like:
200
b'\n\n\n Example Domain\n\n \n \n \n \n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n \n\n\n\n\n\n Example Domain\n This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.\n More information...\n\n\n\n'
'\n\n\n Example Domain\n\n \n \n \n \n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n \n\n\n\n\n\n Example Domain\n This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.\n More information...\n\n\n\n'
{'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'Age': '205214', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Tue, 30 Mar 2021 05:31:24 GMT', 'Etag': '"3147526947"', 'Expires': 'Tue, 06 Apr 2021 05:31:24 GMT', 'Last-Modified': 'Thu, 17 Oct 2019 07:18:26 GMT', 'Server': 'ECS (oxr/8310)', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'Content-Length': '648'}
If the response contains JSON data, we can also access it directly using the .json() method:
import requests
response = requests.get('https://api.github.com/events')
print(response.json())
The above code will output the JSON data returned by the API in Python dictionary format.
That's the basics of working with the response object in Python Requests. There are many more properties and methods available for working with the response object, which can be found in the official Requests documentation.