cURL is a command that is used in command lines and scripts to transfer data over protocols like HTTP, SCP, FTP, SFTP, etc.

It is widely used to make GET, POST, PUT, and DELETE requests over the HTTP protocol.

To make such types of curl requests, you can use requests library in Python.

  • Call requests.get(url) to send a GET request to the specified url.
  • Call requests.post(url, data=dict), with url and data as dict (as a dictionary of keys and values) to send a POST request to the url.
  • Call requests.put(url, data=dict), with url and data as dict (as a dictionary of keys and values) to send a PUT request to the url.
  • Call requests.delete(url, data=dict), with url and data as dict (as a dictionary of keys and values) to send a DELETE request to the url.

Each of the above methods returns a Response object. This object contains status code and response data as Response.status_code and Repsone.text respectivily.

>>> import requests
>>> response = requests.get("https://youtube.com")
>>> response.status_code
200
>>> response.text
<!DOCTYPE html><html>.......</html>

The following example perform curl’s POST request using the requests.post() method.

>>> import requests
>>> payload = {"id": 425, "name": "Pencil Programmer"}
>>> response = requests.post("http://httpbin.org/post", data=payload)
>>> response.status_code
200
>>> response.text
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "id": "425", 
    "name": "Pencil Programmer"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "29", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.13.0", 
    "X-Amzn-Trace-Id": "Root=1-62ceca74-63e5c4037c4e93553f6005dd"
  }, 
  "json": null, 
  "origin": "34.194.221.107", 
  "url": "http://httpbin.org/post"
}

Python cURL Request with Authentication

If you need to perform authorization in Python over the HTTP method, you can pass the required data as a payload to the auth parameter.

import requests
response = requests.get(url, auth=(user, password))

Similarly, if you need to authenticate over HTTP, you can pass the required information such as token as key-value pairs (i.e. dictionary) to the headers parameter of the get method.

import requests
my_headers = {'id': '84528', 'token': 'hds8vh8th%GBBE66eb##$b'}
response = requests.get(url, headers=my_headers)

Python curl request with Search Parameters

An HTTP GET request to an URL often includes search parameters. For example, https://www.google.com/search?q=pencilprogrammer.

These values in params are used by the server to process and return the relevant response for the request.

In Python, you can easily pass such query params with values as a dictionary to the params parameter as follows:

import requests
my_params = {'q': 'pencilprogrammer'}
response = requests.get('https://www.google.com/search', params=my_params)

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply