Working with Response Object of Python Requests Library

The Response object is returned as a result of calls to the request.get(), request.post(), request.put(), and request.delete() methods of the Requests Library. The Requests Library response object contains all information about the server response, including the status code, version number, headers, and cookies. The Requests Library response object includes the content of the server response, such as an HTML page, an image, or a PDF file. In this Python Requests Library Response example, we send a request to ReqBin URL and display the server response. Click Execute to run Python Requests Library Response Example online and see the result.
Working with Response Object of Python Requests Library Execute
import requests

response = requests.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {response.status_code}')
Updated: Viewed: 6196 times

What is HTTP Response?

The server's response confirms that the server has received the request from the client and has processed it. If the server returned a 200x status code, this usually means that the server completed the request successfully. If an error occurs while executing a client request, the server typically responds with a 400x or 500x error message and provides additional information in the body of the message. Server responses usually come in HTML, plain text, JSON, or XML format.

HTTP Response Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

[JSON Data]

The HTTP response contains:

1. The Status Line, which shows the HTTP version number, a three-digit number indicating the result of the request, and the reason phrase(status text).

HTTP/1.1 200 OK

Where:
  • HTTP/1.1: the HTTP version
  • 200: the status code
  • OK: the reason phrase

2. HTTP headers or server header fields contain information that the client can use to learn more about the response and the server that sent it. This information can help the client display a response to the user, store (or cache) the response for future use, and make additional requests to the server now or in the future.

Content-Type: application/json
Content-Length: 19

3. The body of the HTTP message contains the resource requested by the client from the server. The server does not return a message-body for the HEAD.

What is the Python Requests library?

Requests Library is a most popular Library that makes it straightforward to send HTTP requests using POST, GET and PUT methods, post JSON and XML data, upload files, and submit HTML forms. The Library automatically validates server SSL certificates and supports session cookies and International Domain Names. The Requests Library is established on the urllib3 library and disguises the complexness of making HTTP requests behind a simple API. The Requests Library is not contained in the Python distribution, almost everyone uses Requests Library because the Python code for working with HTTP becomes simple, short, and straightforward.

How to use the Python Requests library?

To install the Python Requests library, run the following command:

Install Python Requests Library
pip install requests

After installing the Request Library, you can use it in your work:

import requests

See also