How to send a POST request in cURL
HTTP POST requests are used to send data to the server. The data sent to the server is stored in the request body of the HTTP request. The data could be in the form of a query string or a JSON object. The server then processes the data and sends a response back to the client.
cURL supports sending data using the POST method. You can specify the request method and set data to send along with the request. You can also specify a file to upload with the request.
You can use cURL to send a POST request to a server to test your API, automate a task, or get data from a web page that requires authentication. You can also use cURL to send a POST request to upload a file to a server.
Steps to send a POST request using cURL:
-
Open the terminal.
-
Specify the POST method for the request.
$ curl -X POST https://www.example.com/post-endpoint
-
Provide data for the POST request.
$ curl -X POST -d "key1=value1&key2=value2" https://www.example.com/post-endpoint
-
Send POST request with headers.
$ curl -X POST -H "Authorization: Bearer YOUR_TOKEN" -d "data=value" https://www.example.com/api/v1/resource
-
Set the content type header and provide JSON formatted data to send data as JSON.
$ 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.
-
Upload a file using POST.
$ curl -X POST -F "file=@/path/to/your/file.jpg" https://www.example.com/upload-endpoint
-
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