Python Requests - Send HTTP GET Request
Python - Send HTTP GET Request
HTTP GET request is used to request data from a specified resource. Using Python Requests library, you can make an HTTP GET request.
In this tutorial, we shall learn how to send an HTTP GET request for a URL. Also, we shall learn about the response and its components.
Examples
1. Send GET Request
In this example, we shall use the requests library and send a GET request to a specified URL.
Python Program
import requests
response = requests.get('https://example.com/python-basic-examples/')
Explanation:
- The
requests.get()
function sends an HTTP GET request to the URL passed as a parameter. - The function returns a Response object, which contains various details about the request and response such as status code, headers, cookies, etc.
Let us print the headers that we received in the response to the GET request made in the above example.
Python Program
import requests
response = requests.get('https://example.com/python-basic-examples/')
print(response.headers)
Output
{'Date': 'Mon, 25 Mar 2019 06:46:46 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '8053', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=30', 'Server': 'Apache/2', 'X-Powered-By': 'PHP/5.6.30', 'Link': '</wp-json/>; rel="https://api.w.org/", </?p=317>; rel=shortlink', 'Last-Modified': 'Mon, 25 Mar 2019 06:46:46 GMT', 'ETag': '"d8b82075ab21a19ac91f6ff4579b3722"', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Referrer-Policy': 'no-referrer-when-downgrade'}
Information about HTTP GET Request
Following are some of the points that can be noted while working with HTTP GET requests:
- GET requests can be cached.
- GET requests remain in the browser history.
- GET requests can be bookmarked.
- GET requests should never be used when dealing with sensitive data.
- GET requests have length restrictions.
- GET requests are only used to request data (not modify).
2. Handling Query Parameters in GET Request
HTTP GET requests can include query parameters in the URL to pass additional data to the server. Let’s see how to send GET requests with query parameters.
Python Program
import requests
params = {'search': 'python', 'page': 2}
response = requests.get('https://example.com/search', params=params)
print(response.url)
Explanation:
- In this example, we are sending a GET request with query parameters using the
params
argument in therequests.get()
function. - The
params
dictionary contains key-value pairs representing the query parameters. - The URL is automatically constructed by appending the query parameters to the URL, resulting in a URL like:
https://example.com/search?search=python&page=2
Output
https://example.com/search?search=python&page=2
Summary
In this tutorial, we learned how to send an HTTP GET request and receive a response using the Python requests
library. We also covered sending GET requests with query parameters to pass additional data. Remember that GET requests are used to retrieve data and should never be used to modify data.