Skip to main content
Sending HTTP POST Requests:

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

  1. curl Invokes the curl command line client.

  2. --data '' Specifies an empty data payload. The presence of --data tells curl to use the HTTP POST method.

  3. https://httpbin.org/post The 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

  1. curl Starts the curl program.

  2. -X POST Uses the -X short option to define the request method as POST.

  3. https://httpbin.org/post The 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

  1. curl Executes cURL.

  2. --request POST Explicitly sets the HTTP method to POST.

  3. https://httpbin.org/post Destination 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

  1. curl Runs the curl client.

  2. --data "username=alice&active=true" Sends key-value pairs encoded as application/x-www-form-urlencoded.

  3. https://httpbin.org/post The 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

  1. curl Invokes curl.

  2. --data "username=alice" Adds the first form field.

  3. --data "active=true" Adds an additional form field. cURL combines all --data values.

  4. https://httpbin.org/post Target 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

  1. curl Invokes curl.

  2. --form "description=sample file" Adds a text field to the multipart form body.

  3. --form "file=@example.txt" Uploads a local file. The @ symbol tells cURL to read from a file.

  4. https://httpbin.org/post Receives 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

  1. echo "raw input data" Writes text to standard output.

  2. | Pipes the output to the curl command.

  3. curl --data @- The @- value tells cURL to read POST data from stdin.

  4. https://httpbin.org/post Endpoint 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

  1. curl Invokes curl.

  2. --header "Content-Type: application/xml" Sets the HTTP header to indicate XML content.

  3. --data @payload.xml Reads the request body from an XML file.

  4. https://httpbin.org/post Receives 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

  1. curl Invokes curl.

  2. --header "Content-Type: text/csv" Declares the payload format as CSV.

  3. --data @data.csv Loads CSV content from a local file.

  4. https://httpbin.org/post Target 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

  1. curl Invokes curl.

  2. --header "Content-Type: application/json" Specifies JSON as the request body format.

  3. --data @payload.json Reads JSON data from a file.

  4. https://httpbin.org/post The 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:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

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.

All Topics

Random Quote

“If in 10 years, robots will have taken over and killed us all, I was wrong.”

Linus Torvalds  Finnish software engineer, creator of the Linux kernel and GitZDNET, - IT quotes