How to Send a cURL POST Request
If you have ever needed to send data to a server or interact with an API, you have probably come across curl
. This powerful command-line tool allows you to communicate with servers quickly and easily. In this article we will look together at how to use curl
to perform POST requests. Whether you are a novice developer or a professional, you will find these practical examples useful.
1. What is a POST Request?
A POST request is one of the most widely used methods in the HTTP protocol for sending data to the server. Unlike GET requests, which are used to obtain information, POST is designed to send data such as JSON, forms or even files.
2. POST Curl: Practical Examples
Let's look at some concrete examples to understand how to use curl
to send POST requests:
2.1 POST Request with JSON Data
curl -X POST https://example.com/api \
-H "Content-Type: application/json" \
-d '{"key": "value"}'
How it works:
-X POST
: Indicates that you are using the POST method.--header
,-H
: Specifies HTTP headers, e.g., content type.--data
, -d: Sends data to the server.
2.2 POST Curl with Custom Header
curl -X POST https://example.com/api \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"key": "value"}'
When to use it:This type of request is useful for accessing secure APIs that require an authentication token.
2.3 POST with form data
curl -X POST https://example.com/form \
-d "field1=value1" \
-d "field2=value2"
Alternatively with fields sent together and POST
implied:
curl --data "param1=value1¶m2=value2" https://example.com/resource.cgi
Practical application: Perfect for sending data from web forms.
2.4 File Upload with POST curl
For sending a single file:
curl --form "[email protected]" https://example.com/resource.cgi
With additional files and fields:
curl --form "[email protected];filename=desired-filename.txt" \
--form param1=value1 \
--form param2=value2 https://example.com/resource.cgi
What it does:
--form
: Specifies file submission.file=@
: Specifies file to be uploaded. Remember to provide the correct path.filename
: Change the file name as seen by the server.
2.5 POST request without Data
With empty data:
curl --data '' https://example.com/resource.cgi
Specifying only POST method:
curl -X POST https://example.com/resource.cgi
curl --request POST https://example.com/resource.cgi
When to use it:To send requests to the server without sending explicit data
2.6 Requests with Large Files
When working with large files, you can add parameters to show the progress bar:
curl --tr-encoding -X POST -v --progress-bar -o output -T filename.dat \
https://example.com/resource.cgi
Notes:
-T
: Specifies the file to be uploaded.-or output
: Required to display the progress bar.-v
: Shows more details about the request in verbose mode
--progress-bar
, -#
: Displays the progress bar.curl -X POST https://api.example.com/resource \
-H "Content-Type: application/json" \
-d '{"name": "example", "type": "test"}'
Typical Usage: Creating or modifying resources in a RESTful application.
FAQ
1. How can I check if my POST request was successful?
You can add the -i
option to the command to view the response header and check the HTTP status code.
2. curl
does it support authentication?
Absolutely. You can use -u username:password
for basic authentication or -H
to manage authentication tokens.
3. How do I send multiple JSON data in a POST request?
Just include them all in the -d
option, formatted as a JSON object.
4. Is it possible to send POST requests via HTTPS?
Yes, curl
automatically handles HTTPS. To test non-secure environments, you can use the --insecure
option, although it is not recommended in production.
5. Where do I find more information about curl
?
Consult the Official Curl documentation for more details.
Are you looking for support for REST APIs?
We are ready to help you with optimization and debugging. Visit our Cloud Services. Now 30m of consulting for Free!
Conclusion
With curl
, sending POST requests is simple and powerful. Whether you are working with REST APIs or automating processes, this tool is a valuable ally. Try the commands presented and see how they can simplify your daily operations.