How to make POST requests using cURL
Summary
This article explains multiple ways to send HTTP POST requests with cURL. You learn how to post empty bodies, form fields, files, stdin data, and REST payloads like JSON, XML, and CSV.
Introduction #
cURL is a command line tool and library for transferring data using URLs. This article focuses on sending HTTP POST requests with cURL using different data sources and formats.
cURL empty POST requests #
Empty request using --data ' ' #
This command sends an HTTP POST request with an empty request body to the target URL.
curl --data '' https://httpbin.org/post
Command line breakdown
curlInvokes thecurlcommand line client.--data ''Specifies an empty data payload. The presence of--datatellscurlto use the HTTP POST method.https://httpbin.org/postThe target URL. httpbin.org is a public service that returns request details in the response.
Empty request using -X POST #
This command explicitly sets the HTTP request method to POST without sending a request body.
curl -X POST https://httpbin.org/post
Command line breakdown
curlStarts thecurlprogram.-X POSTUses the-Xshort option to define the request method as POST.https://httpbin.org/postThe endpoint that receives the POST request.
Empty request using --request POST #
This command is functionally identical to -X POST but uses the long option form.
curl --request POST https://httpbin.org/post
Command line breakdown
curlExecutes cURL.--request POSTExplicitly sets the HTTP method to POST.https://httpbin.org/postDestination URL for the request.
cURL POST requests with data #
Data fields #
This command sends URL-encoded form data as part of an HTTP POST request.
curl --data "username=alice&active=true" https://httpbin.org/post
Command line breakdown
curlRuns thecurlclient.--data "username=alice&active=true"Sends key-value pairs encoded asapplication/x-www-form-urlencoded.https://httpbin.org/postThe server endpoint that receives and echoes the data.
Individual data fields #
This command sends multiple data fields by repeating the --data option.
curl \
--data "username=alice" \
--data "active=true" \
https://httpbin.org/post
Command line breakdown
curlInvokescurl.--data "username=alice"Adds the first form field.--data "active=true"Adds an additional form field. cURL combines all--datavalues.https://httpbin.org/postTarget URL for the POST request.
Data fields and data file #
This command sends multipart form data, including a file, using HTTP POST.
curl \
--form "description=sample file" \
--form "file=@example.txt" \
https://httpbin.org/post
Command line breakdown
curlInvokescurl.--form "description=sample file"Adds a text field to the multipart form body.--form "file=@example.txt"Uploads a local file. The@symbol tells cURL to read from a file.https://httpbin.org/postReceives the multipart POST request.
Data from stdin #
This command reads data from standard input (stdin) and sends it as the POST request body.
echo "raw input data" | curl --data @- https://httpbin.org/post
Command line breakdown
echo "raw input data"Writes text to standard output.|Pipes the output to thecurlcommand.curl --data @-The@-value tells cURL to read POST data from stdin.https://httpbin.org/postEndpoint that processes the POST request.
cURL RESTful POST from data files #
From XML file #
This command sends Extensible Markup Language (XML) data from a file in a Representational State Transfer (RESTful) HTTP POST request.
curl \
--header "Content-Type: application/xml" \
--data @payload.xml \
https://httpbin.org/post
Command line breakdown
curlInvokescurl.--header "Content-Type: application/xml"Sets the HTTP header to indicate XML content.--data @payload.xmlReads the request body from an XML file.https://httpbin.org/postReceives the RESTful POST request.
From CSV file #
This command posts Comma-Separated Values (CSV) data from a file.
curl \
--header "Content-Type: text/csv" \
--data @data.csv \
https://httpbin.org/post
Command line breakdown
curlInvokescurl.--header "Content-Type: text/csv"Declares the payload format as CSV.--data @data.csvLoads CSV content from a local file.https://httpbin.org/postTarget endpoint for the POST request.
From JSON file #
This command sends JavaScript Object Notation (JSON) data as a RESTful POST request.
curl \
--header "Content-Type: application/json" \
--data @payload.json \
https://httpbin.org/post
Command line breakdown
curlInvokescurl.--header "Content-Type: application/json"Specifies JSON as the request body format.--data @payload.jsonReads JSON data from a file.https://httpbin.org/postThe URL that accepts and returns the POST data.
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
What does HTTP POST mean?
HTTP POST is an HTTP method used to send data to a server as part of a request body, often to create or process a resource.
Does curl automatically use POST when --data is set?
Yes. When you use --data, cURL automatically switches the request method to HTTP POST unless you override it.
What is the difference between --data and --form?
--data sends data as application/x-www-form-urlencoded by default, while --form sends multipart form data suitable for file uploads.
Can curl send JSON files in POST requests?
Yes. You can send JSON from a file by combining --data with --header 'Content-Type: application/json'.
Further readings #
Sources and recommended, further resources on the topic:
- cURL: Official documentation
- cURL manual:
-d/--dataoption - cURL manual:
-F/--formoption - cURL manual:
-X/--requestoption - cURL cheat sheet
License
How to make POST requests using cURL by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-make-post-requests-with-curl">How to make POST requests using cURL</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.