The python requests module is useful to perform the HTTP requests like GET, POST, DELETE, etc., to get/post data to the specified URL.
Before you start using the requests module, you need to install the requests module in the PIP location. To download and install the requests module, open your command prompt, and navigate your PIP location and type the pip install requests
command.
After completion of installing the requests module, your command-line interface will be as shown below.
If you already open the python interpreter, close it and reopen the interpreter to sync with the latest changes. After installing the requests module, you can use it in your applications by importing the requests module, as shown below.
Following is the example of using the requests module to send the HTTP GET request to the specified URL in python.
The above requests module example will print the response based on the result object that we received from the HTTP request.
When you execute the above python example, it will return the result as shown below.
The response object that we received from the HTTP request will have different properties/methods like status_code
, content
, text
, etc., to perform different operations based on our requirements.
Property/Method | Description |
---|---|
status_code | It will return the status code that will indicate the status of a request. |
content | It will return the response content in bytes. |
text | It will return the response content in Unicode format. |
ok | It will return True if status_code is less than 400 otherwise False. |
reason | It will return the text based on the status code. |
request | It will return the request object that is used to get the response. |
headers | It will return the response headers in dictionary format. |
url | It will return the response URL. |
links | It will return the header links. |
next | It will return the PreparedRequest object for the next request in a redirection. |
apparent_encoding | It will return the apparent encoding. |
cookies | It will return the cookiejar object with the cookies that were received from the server. |
elapsed | It will return the time elapsed during sending the request and receiving the response. |
encoding | It will return the encoding that is used to decode the response text. |
history | It will return the list of response objects holding the history of the request. |
is_permanent_redirect | It will return True if the response is the permanent redirected url otherwise, False. |
is_redirect | It will return True if the response is redirected otherwise, False. |
raise_for_status() | If any error occurs, it will return the HTTPError object. |
iter_content() | It will iterate over the response. |
iter_lines() | It will iterate over the lines of response. |
json() | It will return the response object in dictionary format. |
close() | It is useful to close the server connection. |
Like the HTTP GET method, the request module has different methods to perform HTTP operations based on your requirements.
Method | Description |
---|---|
get(url, params, args) | Useful to make GET requests to the specified URL. |
post(url, data, json, args) | Useful to make a POST request to the specified URL. |
put(url, data, args) | Useful to make a PUT request to the specified URL. |
patch(url, data, args) | Useful to send a PATCH request to the specified URL. |
delete(url, args) | Useful to send a DELETE request to the specified URL. |
options(url, args) | Useful to make an OPTIONS request to the specified URL. |
head(url, args) | Useful to make a HEAD request to the specified URL and return the HTTP headers. |
request(method, url, args) | Useful to request the specified method to the specified URL. |
Following is the example of using the different HTTP methods in the requests module.
The HTTP GET method will send the parameters in the query string using the params parameter. The POST, PUT, and PATCH methods will send the payload through the message body using data parameters.
This is how you can use the requests module in python to get the required data from other URLs using HTTP methods such as GET, POST, PUT, PATCH, etc., based on your requirements.