Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

-d, --data <data> Send specified data in POST request. Details provided below.

-f, --fail Fail silently (don't output HTML error form if returned).

-F, --form <name=content> Submit form data.

-H, --header <header> Headers to supply with request.

-i, --include Include HTTP headers in the output.

-I, --head Fetch headers only.

-k, --insecure Allow insecure connections to succeed.

-L, --location Follow redirects.

-o, --output <file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.

-O, --remote-name Write output to file named like the remote file (only writes to current directory).

-s, --silent Silent (quiet) mode. Use with -S to force it to show errors.

-v, --verbose Provide more information (useful for debugging).

-w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w "\n" (can add to ~/.curlrc).

-X, --request The request method to use.


When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:

  • application/json
  • application/x-www-form-urlencoded

Many APIs will accept both formats, so if you're using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because

  • the json format requires a bunch of extra quoting
  • curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set

This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests.

curl usage

For sending data with POST and PUT requests, these are common curl options:

  • request type

    • -X POST
    • -X PUT
  • content type header

  • -H "Content-Type: application/x-www-form-urlencoded"

  • -H "Content-Type: application/json"

  • data

    • form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt
    • json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json


POST application/x-www-form-urlencoded

application/x-www-form-urlencoded is the default:

curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data


curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

with a data file

curl -d "@data.txt" -X POST http://localhost:3000/data

POST application/json

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

with a data file

curl -d "@data.json" -X POST http://localhost:3000/data
"name": "postdemo",
"version": "1.0.0",
"scripts": {
"start": "node server.js"
"dependencies": {
"body-parser": "^1.15.0",
"express": "^4.13.4"
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded'/data', function (req, res) {

very helpful, I used curl to automate some tests written in bash script files

Thank you... Very instructive.

Helpful! Many thanks

Vanshita commented Jun 9, 2017

While running the following command from cmd. It gives the error as '-d' is not recognized as an internal or external command,
operable program or batch file.

Command - >curl -d '{"name": "Abby Mallard", "original_voice_actor": "Joan Cusack", "animated_debut": "Chicken Little"}' -H "Content-Type: application/json" -XPOST http://localhost:9200/disney/character/1

Could anyone help me figure out the mistake?

pushyamig commented Jun 22, 2017 edited

IS there a way to do a POST one line at a time from the file that contain array of json events: one JSON per line. I have currently 50K JSON events that i need to post to a service. I felt if curl provide any simple solution with out writing a script for making this happen.

curl -d "@data.json" -X POST and that data.json contains array of json strings like

I struggled with successfully posting JSON to my Express endpoint, and I finally got it working. It was necessary to escape the quotes in the submitted JSON:

curl -d {\"key\":\"value\"} -H "Content-Type: application/json" -X POST "http://localhost:3000/myendpoint"

yja1 commented Jul 10, 2017

http post server_url/add_picture?id=1&img=img1
at the same time upload image data throw payload
I do the server part. I don't konw how to post data also with payload image data. and I don't know how to receive. I use fastcgi do the server part.

jopek commented Aug 15, 2017

@AaronBaldwin you could have also used '' like so: curl -d '{"key":"value"}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment