To send a curl post request, we can use the following syntax:
curl -X POST \
-H "Headers" \
-d "Body" \
https://httpbin.dev/post
This snippet demonstrates the fundamental components of a curl post data request:
- -X: Defines the HTTP request method, POST in this instance.
- -H: Used for adding request headers, which are frequently necessary for POST requests.
- -d: Used for including the cURL post body, containing your payload data (JSON, XML, or form data).
Common cURL POST Request Examples
Let’s explore some typical scenarios for sending a curl post request.
cURL POST Request with JSON
To include a JSON body in a cURL POST request with JSON, utilize the -d option followed by the data object:
curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://httpbin.dev/post
In this example, we set the request Content-Type header and include the cURL request body in JSON format. The server returns the parsed request:
{
"args": {},
"headers": {
....
"Content-Type": [
"application/json"
],
},
"method": "POST",
"data": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
"json": {
"key1": "value1",
"key2": "value2"
}
}
cURL POST Request with XML
You can also transmit a cURL POST request with XML by modifying the header and body format:
curl -X POST \
-H "Content-Type: application/xml"
-d "1Box of Chocolate Candy"
https://httpbin.dev/post
This method is frequently used for SOAP-based or legacy APIs that require XML payloads.
Sending Files with cURL POST
To upload files using cURL POST, we use the -F flag to send file data as multipart/form-data:
curl -X POST -F "file=@/file/path/file.img" https://httpbin.dev/post
Note: When uploading multiple files, use multiple -F flags. This approach is useful for form-based file upload endpoints.
Sending Form POST Requests
Form POST requests are frequently required to retrieve tokens or data behind forms. Let’s replicate this form example using cURL POST requests:
curl -X POST \
-d 'custname=Webparsers&custtel=&custemail=&size=small&topping=cheese&delivery=13%3A45&comments=' \
https://httpbin.dev/post
This technique is particularly valuable when interacting with APIs or web applications that use forms to receive input.
Authenticate cURL POST Requests
Let’s configure cURL POST requests to support basic HTTP authentication. For this purpose, we can use the basic option:
curl -X POST --user user:passwd --basic https://httpbin.dev/basic-auth/user/passwd
The response from the above cURL request confirms successful authentication:
{
"authorized": true,
"user": "user"
}
For comprehensive details on various authentication mechanisms with cURL requests, refer to our dedicated guide.
Further cURL Options
Sending cURL POST requests often requires additional configuration for the request. Here are supplementary cURL arguments and their usage:
| Argument | Full Option | Description |
|---|---|---|
| -x | –proxy | Use proxy with cURL, either as HTTP or SOCKS |
| -Z | –parallel | Send multiple cURL requests in parallel |
| -L | –location | Make cURL follow request redirects |
| -O | –output | Save the response binary data to a local directory |
| -I | –head | Use the request HEAD HTTP method |
| -b | –cookie | Add request cookies |
| -v | –verbose | Sends the request in verbose mode, which include further debugging details |
| -k | –insecure | Skip SSL verification for HTTPs connection |
For comprehensive details on sending requests with cURL, refer to our dedicated guide.