Sending HTTP Request with Python Requests Library

To send an HTTP request with the Python Requests Library, you need to use the request.get(url, params) or request.post(url, data, params) methods. You can make HTTP GET, POST, PUT, DELETE and HEAD requests with the Python Request Library, submit forms, download files and images, and set rate limits and timeouts. The Requests library methods take the target URL as the first parameter and several additional parameters. For example, you can pass custom HTTP headers with the "headers" parameter, HTTP cookies with the "cookies" parameter, and user authorization data with the "auth" parameter. In this Python HTTP Requests example, we send a GET request to the ReqBin echo URL with a custom HTTP header. Below you can see more examples of HTTP methods with Python Requests. Click Execute to run Python HTTP Requests Example online and see the result.
Sending HTTP 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: 12858 times

What is the Python Request Library?

Requests Library is a popular Library that makes it easy to send HTTP requests using POST, GET and DELETE methods. The Requests Library is based on the urllib3 library and conceals the complexity of making HTTP requests behind a simple API. The Requests Library automatically validates the server's SSL certificates and supports International Domain Names and session cookies. The Requests Library is not included in the Python distribution, but everyone uses it because the Python code for HTTP becomes short, simple, and straightforward.

How to install the Python Requests Library?

You can install the Requests Library using the pip package installer for Python:

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 Syntax
import requests

How to send HTTP GET request using Python Requests?

The GET request method is used to retrieve information from the server. GET request can be cached, bookmarked, and left in browser history. The GET method must not change the data on the server.

GET request
Python GET Request Syntax
requests.get(URL, parameter={key: value}, arguments)

Where:
  • URL: target URL/API point
  • parameter (optional): a dictionary, list of tuples, or bytes to send in a GET request URL
  • arguments (optional): the arguments that the GET request accepts, such as cookies, auth, cert, timeout, proxies etc

How to send HTTP POST request using Python Requests?

With a POST request, you can send data to the server, including images, file uploads, JSON strings, etc. POST requests cannot be cached, bookmarked, or stored in browser history.

Python POST Request Syntax
requests.post(URL, data=[data], json=[json], arguments)

Where:
  • URL: target URL/API point
  • data (optional): data to send a POST request to the server, this can be a dictionary, a list of tuples, bytes, or a file
  • json (optional): a dictionary that will be converted to a JSON string and included in the body of the POST request
  • arguments (optional): optional arguments that the POST request accepts, such as allow_redirects, cookies, stream, auth, etc

How to send HTTP PUT request using Python Requests?

With a PUT request, you can replace or update an existing resource on the server. The HTTP PUT requests cannot be cached, bookmarked, or stored in browser history.

Python PUT Request Syntax
requests.put(URL, data=[data], arguments)

Where:
  • URL: target URL/API point
  • data (optional): data to send a PUT request to the server, this can be a dictionary, a list of tuples, bytes, or a file
  • arguments (optional): optional arguments that the PUT request accepts, such as cookies, verify, auth, etc

How to send HTTP DELETE request using Python Requests?

With a HTTP DELETE request, you can remove a resource from the server. The DELETE requests cannot be cached, bookmarked, or stored in browser history.

Python DELETE Request Syntax
requests.delete(URL, arguments)

Where:
  • URL: target URL/API point
  • arguments (optional): optional arguments that the DELETE request accepts, such as cookies, auth, timeout, etc.

How to send HTTP HEAD request using Python Requests?

With an HTTP HEAD request, you can do the same things as a GET request, except that HEAD only receives the status line and headers (no response body). The HEAD request cannot be bookmarked nor stored in the browser's history.

Python HEAD Request Syntax
requests.head(URL, arguments)

  • URL: target URL/API point
  • arguments (optional): optional arguments that the HEAD request accepts, such as headers, timeout, cookies etc.

How to send HTTP PATCH request using Python Requests?

With a PATCH request, you can partially change the specified resource on the server. HTTP PATCH is faster and less resource-intensive than the PUT method. It cannot be cached, bookmarked, or stored in browser history.

Python PATCH Request Syntax
requests.patch(URL, arguments)

Where:
  • URL: target URL/API point
  • arguments (optional): optional arguments that the PATCH request accepts, such as cookies, auth, stream, etc.

How to send HTTP OPTIONS request using Python Requests?

The OPTIONS request is used by browsers for CORS operations. OPTIONS describes the communication options available for the requested resource and does not change the data on the server. OPTIONS cannot be cached, bookmarked, or stored in browser history.

Python OPTIONS Request Syntax
requests.options(URL, arguments)

  • URL: target URL/API point
  • arguments (optional): optional arguments that the OPTIONS request accepts, such as timeout, cert, auth, cookies etc.

See also