Python Requests – HTTP GET – Example

Python – Send HTTP GET Request

HTTP GET request is used to request data from a specified resource. Using Python Requests library, you can make a HTTP GET request.

In this tutorial, we shall learn how to send a HTTP GET request for a URL. Also, we shall learn about the response and its components.

Example 1: Send GET Request

In this example, we shall use requests library and send a GET request with a URL.

Python Program

import requests

response = requests.get('https://pythonexamples.org/python-basic-examples/')
Run this program

requests.get() function returns a Response object that contains parameters like html content, cookies, headers, 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://pythonexamples.org/python-basic-examples/')

print(response.headers)
Run this program

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': '<https://pythonexamples.org/wp-json/>; rel="https://api.w.org/", <https://pythonexamples.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 is only used to request data (not modify).

Summary

In this tutorial of Python Examples, we learned how to send HTTP GET request and receive a response using Python requests library.