Python's curl GET request example
Using the curl command in Python is relatively easy. It can be done using the requests library in Python. Here is an example of how to use it:
Code Example 1:
import requests
url = "https://example.com"
response = requests.get(url)
print(response.content)
The above code sends a GET request to the URL specified and stores the response in the response variable. The content attribute of the response object contains the response content in bytes.
Code Example 2:
import requests
url = "https://example.com"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
response = requests.get(url, headers=headers)
print(response.content)
The above code sends a GET request to the URL specified with a custom User-Agent header using the headers parameter.
You can also add query parameters to your GET requests using the params parameter as shown below:
Code Example 3:
import requests
url = "https://example.com"
payload = {"param1": "value1", "param2": "value2"}
response = requests.get(url, params=payload)
print(response.content)
The above code sends a GET request to the URL specified with two query parameters using the params parameter.