Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active March 27, 2024 16:11
Star You must be signed in to star a gist
Save subfuzion/08c5d85437d5d4f00e58 to your computer and use it in GitHub Desktop.
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.

POST

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

Examples

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

explicit:

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
{
"key1":"value1",
"key2":"value2"
}
param1=value1&param2=value2
{
"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
app.post('/data', function (req, res) {
console.log(req.body);
res.end();
});
app.listen(3000);
@anandzhang
Copy link

Thank you very much for your explanation.

@cuncdev
Copy link

cuncdev commented Dec 3, 2019

I grabbed it in a glance ! Thank you for this !

@rjpcasalino
Copy link

@stevedonovan thanks for the tip on --data-binary vs -d (was about to smash my head against the table) 👍

@HammzaHM
Copy link

HammzaHM commented Dec 18, 2019

@subfuzion Thanks a lot it is helpful 👍

@thomaslevesque
Copy link

Thanks!

@WIttyJudge
Copy link

Thank you 😎

@sergnio
Copy link

sergnio commented Aug 14, 2020

Thank you so much :)

In your second Application/JSON example, you're missing a header

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" -H "Content-Type: application/json" -X POST http://localhost:3000/data

@prkagrawal
Copy link

super awesome

@Art2Cat
Copy link

Art2Cat commented Nov 5, 2020

thanks.

@mike239x
Copy link

For me curl 7.58.0 somehow doesn't work properly with json data (even thought I got the header set up), but the other version works, thanks!

@tjcchen
Copy link

tjcchen commented Nov 27, 2020

Thanks a lot!

@garraflavatra
Copy link

Very helpful, thanks!

@callenmas13
Copy link

Very helpful. Thank you.

@gsharew
Copy link

gsharew commented Apr 8, 2021

You saved every one here except one 1 or 2 persons.

Copy link

ghost commented Jul 21, 2021

If you pass -d option then -X POST is not needed because -d implies -X POST. Could you simplify your examples?

@ziyoung
Copy link

ziyoung commented Jul 27, 2021

curl also supports custom dns resolver. Use --resolve to specify target of this request.

curl https://your-website.com --resolve your-website.com:1.2.3.4:443

@macroh92
Copy link

Very helpful!! Thanks!

@4inches-usbstick
Copy link

was very helpful tyvm

@akproject
Copy link

thanks to you i finally understand differences between curl and web browser and http request in each one

@jvandy83
Copy link

This is great. Succinct. To the point. Thanks!

@SPARKILATOR
Copy link

SPARKILATOR commented Nov 23, 2021

Please help with this query.

suppose we have a curl command where we have directed some data query to certain proxy using POST request like for example

curl -X POST -d "validFor=10&organizationName=thecomapnyofyorkshireandleicestershireandenglishcricketboardandutility" -H 'X-HTTP-Proxy-To: https:google.com'

for this command, we are getting error "string too long" so got to know that organisationName is too long and when we are reducing the organisation name length to 60 or less than 60 then it is working fine so is there any limitation related to curl or some other factors are involved via which we can face this issue.?

Please Help.

Thank U.

@SPARKILATOR
Copy link

I am more so interested on that organisationName factor whether we are allowed to enter multiple characters in that or it is limited to certain characters and if some documentation is there related to this then it will be great.

Thank U.

@Joshuauche
Copy link

thanks

@PLViet
Copy link

PLViet commented May 3, 2022

Can someone help me with this?

curl -X 'POST'
'https://subgiare.vn/api/profile/info'
-H 'accept: /'
-H 'api-token: eyJpdiI6IjZuMnc0YkZTamgvYXl4QlZiOHU3R3c9PSIsInZhbHVlIjoiQUFlUDk4azJ6SVQ5d2VuUU50V096dHgzUndabjl6UHhQa0Y3eGVqcHg1RzdNSW9xdHhLR0JVVHZtNHg5VEN2ViIsIm1hYyI6ImM4NGJjNTYzYjkzZjk2MDczOGY1MDc5NDYyYTIzNzcwYzMxNDA1OTI4MDRkYWRhMTY4ZTc1OTg4ZDdmNjE1MjUiLCJ0YWciOiIifQ=='
-d ''

@Rayen-benslimen
Copy link

Hi,

I want to know how i can get the response of this Curl ?
curl -d "email=blabla@gmail.com=&password=blabla" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

@gsharew
Copy link

gsharew commented Jun 8, 2022 via email

@yogananda-muthaiah
Copy link

Anyone have tried with POST Method with body having form-data with zip file upload ?

@iannyFARUE
Copy link

Thanks for the short and detail article

@Arkwright112
Copy link

The -X flag specifies a custom request method to use when communicating with the HTTP server. By default, the GET method is used unless some other method is specified. thank you!!! @slope unblocked

@daothanhhuy
Copy link

Very helpful, thank you so much.

@jimklimov
Copy link

If you pass -d option then -X POST is not needed because -d implies -X POST. Could you simplify your examples?

Technically other HTTP verbs can have data too. Even GET, though discouraged and implementation-dependent to be recognized or passed by transport like proxies.

@KrisMelikova
Copy link

Awesome! Thanks!

@sdsanchezm
Copy link

Great examples, thank you!

@Birdmaaan4
Copy link

Thank you! Helped me figure out a call I was trying to make. Bookmarked

@fakeowl1
Copy link

Good work, thank you!

@hgjkim
Copy link

hgjkim commented Aug 4, 2023

SUPER Thanks!

@mfyuce
Copy link

mfyuce commented Nov 15, 2023

Thanks

@masif2002
Copy link

Sending HTTP requests to https://httpbin.org/anything using curl maybe helpful

@Robson-pds
Copy link

Robson-pds commented Dec 15, 2023

Para Windows 11, passando json ficou:
curl -X POST %url% -H "Content-Type: application/json" -H "Authorization: Bearer %token%" -d "{\"number\":\"%number%\",\"body\":\"%body%\"}" nota que o que esta dentro do %% são variaveis .bat. tem que passar o conteudo do json com o \" porque o windows interpreta de maneira distinta as aspas.

@MohammedSaLah-Eldeen
Copy link

a really perfect cheatsheet

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