Sending GET Request with Python Requests Library

To send an HTTP GET request using the Python Requests library, you must call the requests.get() method and pass the target URL as the first parameter. Additional HTTP headers can be passed to the requests.get() method with the headers= parameter. You cannot send data in the body of an HTTP GET message but still can send some information to the server with the URL parameters. In this Python Requests GET example, we send a GET request to the ReqBin echo URL. Click Execute to run the Python Requests GET example online and see the result.
Sending GET Request with Python Requests Library Execute
import requests

r = requests.get('https://reqbin.com/echo/get/json', 
                 headers={'Accept': 'application/json'})

print(f"Status Code: {r.status_code}, Content: {r.json()}")
Updated: Viewed: 9119 times

What is the Python Request Library?

Requests Library is a popular library for sending HTTP requests (POST, GET, DELETE, etc) in Python. The Requests library provides a set of handly methods that make it easy to work with HTTP protocol. The Requests library can automatically validate server SSL certificates, supports Client-Side certificates, and has built-in support for International Domain Names, URLs, and Unicode response bodies. The Requests Library makes it easy to send GET and POST requests to protected resources that require Basic or Digest user authentication, can automatically handle session cookies, and automatically decompress the response content. Python has a built-in urllib3 module for handling HTTP requests with similar functionality, but almost all use Python Requests as the code is simple and easy to read. However, the Requests Library is not included in the Python distribution.

What is HTTP GET request?

The GET request method is used to request a resource from the server using the provided URL. The HTTP GET method is one of nine standard methods of Hypertext Transfer Protocol (HTTP). The GET method should only be used to retrieve data from the server. GET requests cannot send data to the server in the body of a GET message and cannot change the server's state. If you want to change data on the server, use POST, PUT, PATCH, or DELETE methods.

HTTP GET Request Example
GET /echo HTTP/1.1
Host: reqbin.com
Accept: */*

How to install the Python Requests Library?

To install the Python Requests Library, you need to run the following command:

Install Python Requests Library
pip install requests

After installing the Requests Library, you can use it in your code by importing the Requests Library with the following Python code:

import requests

Python Requests GET Method Syntax

The general form of the Python Requests method for making a GET request is as follows:

Python Requests GET Syntax
requests.get(URL, headers=[headers], cookies=[cookies], auth[auth], timeout[timeout], arguments)

Where:
  • URL: target URL/API point.
  • headers (optional): a list of HTTP Headers to send to the server.
  • cookies (optional): a list of HTTP Cookies to send to the server.
  • auth (optional): a tuple with the user authentication details.
  • timeout (optional): a number or tuple with connection, send or receive timeouts.
  • arguments (optional): other arguments that the GET request accepts.

How to make a GET request with Python Requests Library?

To send a GET request, you need to call the requests.get() method and pass the target URL as a parameter:

Python GET Example using the Requests Library
import requests

r = requests.get('https://reqbin.com/echo/get/json')
      
print(r.status_code)

How to send additional HTTP headers with Python GET request?

To send additional HTTP headers with a Python GET request using the Requests Library, pass custom headers to the requests.get() method with the headers= parameter:

Python GET Request with Custom HTTP Headers
import requests

r = requests.get('https://reqbin.com/echo/get/json', headers={'Accept': 'application/json'})
      
print(r.headers)

How to send HTTP cookies with Python GET request?

To send HTTP Cookies to the server using the Requests Library, pass the cookies to the requests.get() method with the cookie= parameter:

Python GET Requests with Сookies
import requests
  
r = requests.post('https://reqbin.com/echo/get/json', cookies={"key": "value"})
  
print(r.text)

How to send user authentication credentials with Python GET Request?

To send credentials to authenticate a user with Python GET request, pass the credentials to requests.get() using the auth= parameter:

GET Requests with Authentication Credentials Example
import requests
from requests.auth import HTTPBasicAuth

r = requests.get('https://reqbin.com/echo/get/json', auth=HTTPBasicAuth('login', 'password'))
              
print(r.text)

See also