curl request python script

Curl Request Python Script

If you want to make an HTTP request from a Python script, you can use the requests library. However, if you want to make a request using the command-line tool curl, you can also do that from your Python script by using the subprocess module.

Using the requests Library

If you want to make an HTTP request using the requests library, here's an example:


import requests

url = "https://example.com/api/endpoint"
payload = {"key1": "value1", "key2": "value2"}
headers = {"Authorization": "Bearer your_access_token"}

response = requests.get(url, params=payload, headers=headers)

print(response.content)
    

In this example, we're making a GET request to https://example.com/api/endpoint with two query parameters (key1=value1 and key2=value2) and an authorization header with a bearer token.

Using the subprocess Module

If you want to make a request using curl, you can use the subprocess module to spawn a new process and execute a shell command. Here's an example:


import subprocess

url = "https://example.com/api/endpoint"
payload = "key1=value1&key2=value2"
headers = "Authorization: Bearer your_access_token"

response = subprocess.check_output(["curl", "-X", "GET", "-G", url, "-d", payload, "-H", headers])

print(response.decode())
    

In this example, we're making a GET request to https://example.com/api/endpoint with two query parameters (key1=value1 and key2=value2) and an authorization header with a bearer token.