How do I get the response object in Python Requests?
The request.Response object is returned as the result of requests.get(), request.post(), request.put(), and request.delete() call. The HTTP response contains all information about the server response, such as status code, version number, headers, and cookies. The Requests library response object includes the content of the HTTP response, such as an HTML code, image, or PDF file, and allows the browser to display the server's response to the user. In this Python Requests Response example, we send a request to ReqBin URL and display the response status code. Click Execute to run Python Response Example online and see the result.
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
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
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.
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: