curl equivalent in python requests

curl equivalent in python requests

As a developer, you might have used the command-line tool called cURL to send HTTP requests to a server. Python also has a similar library called Requests that allows you to send HTTP requests to a server. In this post, we will learn how to use Python Requests to send HTTP requests and the equivalent of cURL commands in Python Requests.

What is cURL?

cURL is a command-line tool that allows you to transfer data from or to a server, using various protocols like HTTP, HTTPS, FTP, etc. You can use cURL to test RESTful API endpoints, download files, and perform other tasks that involve sending or receiving data over the internet.

Python Requests library

Python Requests is a third-party library that allows you to send HTTP/1.1 requests using Python. It is designed to be simple and easy to use while at the same time providing advanced features like session handling, authentication, and proxies.

Syntax for sending HTTP requests in Python Requests

The syntax for sending an HTTP request using Python Requests is as follows:


    import requests
    
    response = requests.get('https://www.example.com')
  

In the above example, we are importing the Requests library and making a GET request to 'https://www.example.com'. The response object returned by the GET request contains the HTTP status code, headers, and content.

cURL equivalent in Python Requests

Now let's see some of the commonly used cURL commands and their equivalent in Python Requests.

1. Sending a GET request

The following cURL command sends a GET request to 'https://www.example.com':


    curl https://www.example.com
  

The equivalent Python Requests code would be:


    import requests
    
    response = requests.get('https://www.example.com')
  

2. Sending a POST request

The following cURL command sends a POST request to 'https://www.example.com' with a JSON payload:


    curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://www.example.com
  

The equivalent Python Requests code would be:


    import requests
    import json
    
    payload = {'key1': 'value1', 'key2': 'value2'}
    headers = {'Content-Type': 'application/json'}
    
    response = requests.post('https://www.example.com', data=json.dumps(payload), headers=headers)
  

3. Sending a PUT request

The following cURL command sends a PUT request to 'https://www.example.com' with a JSON payload:


    curl -X PUT -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://www.example.com
  

The equivalent Python Requests code would be:


    import requests
    import json
    
    payload = {'key1': 'value1', 'key2': 'value2'}
    headers = {'Content-Type': 'application/json'}
    
    response = requests.put('https://www.example.com', data=json.dumps(payload), headers=headers)
  

4. Sending a DELETE request

The following cURL command sends a DELETE request to 'https://www.example.com' with a JSON payload:


    curl -X DELETE -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://www.example.com
  

The equivalent Python Requests code would be:


    import requests
    import json
    
    payload = {'key1': 'value1', 'key2': 'value2'}
    headers = {'Content-Type': 'application/json'}
    
    response = requests.delete('https://www.example.com', data=json.dumps(payload), headers=headers)
  

Conclusion

In this post, we have learned how to use Python Requests to send HTTP requests and the equivalent of cURL commands in Python Requests. Python Requests is a powerful library that allows you to interact with web services in a simple and easy way. Whether you are testing RESTful API endpoints or downloading files, Python Requests can make your life easier.