HTTP POST is a method used to submit data to be processed to a specified resource on the web. cURL is a versatile tool that allows you to send and retrieve data from servers. It supports a wide range of protocols, including HTTP, HTTPS, FTP, and many more.

In development and testing scenarios, it is common to interact with APIs or web services that require data submission using the POST method. With cURL, you can easily simulate these requests, providing headers, data, or files as needed.

Knowing how to send a POST request with cURL can help in debugging APIs, submitting forms from command line, and automating tasks that require web data submission.

Steps to send a POST request using cURL:

  1. Open the terminal.
  2. Use the -X POST option with cURL to specify the POST method.
  3. Provide data for the POST request using the -d or –data option.
    $ curl -X POST -d "key1=value1&key2=value2" https://www.example.com/post-endpoint
  4. Send POST request with headers using the -H or –header option.
    $ curl -X POST -H "Authorization: Bearer YOUR_TOKEN" -d "data=value" https://www.example.com/api/v1/resource
  5. To send data as JSON, set the content type header and provide JSON formatted data.
    $ curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://www.example.com/api/json-endpoint

    Remember to properly format your JSON data and ensure that strings are enclosed in double quotes.

  6. Upload a file using POST with the -F option.
    $ curl -X POST -F "file=@/path/to/your/file.jpg" https://www.example.com/upload-endpoint
  7. Combine multiple options as needed.
    $ curl -X POST -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{"key":"value"}' https://www.example.com/api/endpoint
Discuss the article:

Comment anonymously. Login not required.