cURL -d Option with Examples


In order to send data via an HTTP POST request in cURL, you need to make use of the -d or the --data option.


From cURL manual/help page:

 -d, --data <data>          HTTP POST data
              separating &-symbol. Thus, using '-d name=daniel -d skill=lousy'
               curl -d "name=curl" https://example.com
               curl -d "name=curl" -d "tool=cmdline" https://example.com
               curl -d @filename https://example.com

Example 1: Sending Login Form Details using -d option

    curl -X POST -d "userid=EMP101&password=thepassword" https://code2care.org/employee/login
    

    As you can see, we have provided the form data with -d flag and passing the userid and password details as key-value pairs like a query string.

    If you want you can send the form body data parameters as separate key and value pairs, you can do it as follows,

    curl -X POST -d "userid=EMP101" -d "password=thepassword" https://code2care.org/employee/login
    

Example 2: Sending JSON data to REST API POST Request

    curl -X POST --data '{"product_id": 1010, "search_query": "iPhone 15", "user_id":"EMP101"}' 
     -H "Content-Type: application/json" 
     https://code2care.org/api/product-details/
    

    In the above example, we are sending the JSON data to an HTTP POST REST API using the --data option.

    Note: We have to set the Content-Type header as application/json in this case.


Example 3: Sending Data from a file using -d option

    curl -X POST -d "@upload-batch_2023-07-01.csv" https://code2care.org/financial-data/upload-job/
    

    As you can see in the above example we are reading the data from a file and using cURL to upload its content to a POST API Request.

cURL -d or --data flag example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap