How to use curl POST method to submit data to a server?

Published on Aug. 22, 2023, 12:19 p.m.

To use the curl command to submit data to a server using the POST method, you can use the -d flag followed by the data you want to send. Here is an example:

curl -X POST -d 'param1=value1&param2=value2' https://www.example.com/api/endpoint

In this example, we’re sending a POST request to https://www.example.com/api/endpoint with some data. The -X flag is used to specify the HTTP method as POST, and the -d flag is used to specify the data we want to send in the request body.

The data is provided as a string in the format key1=value1&key2=value2, where each key-value pair is separated by an ampersand (&) and the key and value are separated by an equals sign (=).

When you run this command, curl will make a POST request to the specified endpoint with the provided data. If the request is successful, the server should respond with a status code indicating success, and any response data will be printed to the console.

Note that the specific format of the data you need to provide depends on the requirements of the API or server you are working with. You may need to consult the documentation or contact the server owner for more information on how to format your data properly.

Additionally note that the -X POST -d flags are a single command-line argument and you should include the URL of the API endpoint after them.

Lastly, ensure you update the URL and data parameters as appropriate for your use case.

Tags: