Create a gist now

Instantly share code, notes, and snippets.

Embed
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.

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);
@espaciomore

This comment has been minimized.

Show comment
Hide comment
@espaciomore

espaciomore Feb 3, 2017

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

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

@dianjuar

This comment has been minimized.

Show comment
Hide comment
@dianjuar

dianjuar Mar 22, 2017

Thank you... Very instructive.

Thank you... Very instructive.

@rrdennis

This comment has been minimized.

Show comment
Hide comment
@rrdennis

rrdennis Apr 19, 2017

Helpful! Many thanks

Helpful! Many thanks

@Vanshita

This comment has been minimized.

Show comment
Hide comment
@Vanshita

Vanshita Jun 9, 2017

Hey.
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?

Vanshita commented Jun 9, 2017

Hey.
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

This comment has been minimized.

Show comment
Hide comment
@pushyamig

pushyamig Jun 22, 2017

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 http://xyx.org and that data.json contains array of json strings like
{"sensor":"id1","sendTime":"2017-05-07T02:35:16.385Z"}
{"sensor":"id2","sendTime":"2017-05-07T02:35:16.385Z"}

pushyamig commented Jun 22, 2017

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 http://xyx.org and that data.json contains array of json strings like
{"sensor":"id1","sendTime":"2017-05-07T02:35:16.385Z"}
{"sensor":"id2","sendTime":"2017-05-07T02:35:16.385Z"}

@AaronBaldwin

This comment has been minimized.

Show comment
Hide comment
@AaronBaldwin

AaronBaldwin Jun 25, 2017

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"

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

This comment has been minimized.

Show comment
Hide comment
@yja1

yja1 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.

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

This comment has been minimized.

Show comment
Hide comment
@jopek

jopek Aug 15, 2017

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

jopek commented Aug 15, 2017

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

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Oct 21, 2017

See additional information into free book: https://ec.haxx.se/curl-name.html

ghost commented Oct 21, 2017

See additional information into free book: https://ec.haxx.se/curl-name.html

@abhishekshukla2017

This comment has been minimized.

Show comment
Hide comment
@abhishekshukla2017

abhishekshukla2017 Oct 30, 2017

@Vanshita you might have not set the path , set the environment path and try.

@Vanshita you might have not set the path , set the environment path and try.

@adarshdp

This comment has been minimized.

Show comment
Hide comment
@adarshdp

adarshdp Nov 2, 2017

my rest api is not accepting data what i m sending help me out

here code---

"50", "DistrictId" => "26123","taluk_id" => "26103", "taluk_name" => "kattreguppe","Address" => "bangluru", "PhoneNo" => "963258788"); function LogError($error) { $file = fopen("C:/xampp/htdocs/unittest/log/taluk.txt","a"); $t=time(); $t1=date("d-M-Y H:i:s: ", $t); fwrite($file,$t1); fwrite($file,$error); fwrite($file,"\r\n"); fclose($file); } function get_data($url,$returndata) { // $arry= implode(":",($returndata)); //curl_getinfo($ch, CURLINFO_HTTP_CODE); //$data=http_build_query($returndata); $data_json = json_encode($returndata,JSON_PRETTY_PRINT); //$data_json = json_encode(array("customer"=>$data)); LogError($data_json); //var_dump($data_json); //echo $data_json; //$ch = curl_init('http://api.local/rest/users'); /* $json_output = json_encode($returndata, JSON_UNESCAPED_SLASHES); $url = 'http://192.168.1.181/Omps/Views/Mpcs/Services/CreateTaluk/='.urlencode($json_output)'; */ $dat1='{"UnionId":55,"DistrictId":4521,"taluk_id":5252,"taluk_name":adars,"Address":bgnlr,"PhoneNo":9664445}'; $dat="UnionId=55&DistrictId=4521&taluk_id=5252&taluk_name=adars&Address=bgnlr&PhoneNo=9664445"; /* $postData = '';    //create name value pairs seperated by &    foreach($returndata as $key => $value)    {       $postData .= $key . '='.$value.'&';    }    $postData = rtrim($postData, '&'); */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_HTTPHEADER,array ("Accept: application/json",'Content-Type: application/json','Content-Length: ' . strlen($data_json))); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); //$data_json = urlencode(json_encode($returndata)); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //curl_setopt($ch, CURLOPT_POSTFIELDS,array("customer"=>$data_json)); //curl_setopt($ch, curl_setopt_array,$data_json); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; } // $answer="-*- MPCS Name: ".$UnionId ." MPCS ID: ". $DistrictId." Shift: ".$taluk_id; /* $formdata = array( 'UnionId'=> 1, 'DistrictId'=> 1111, 'taluk_id'=> 3333, 'taluk_name'=> 'abas', 'Address'=> 'bngl', 'PhoneNo'=> 98586598596 ); */ /*$formdata = Array ('UnionId' => 3,'DistrictId' => 260532,'taluk_id' => 26014232,'taluk_name' => 'dvg42','Address' => 'banglore42','PhoneNo' => 96327117652);*/ $formdata = Array ( '[UnionId]' => 3, '[DistrictId]' => 260532, '[taluk_id]'=> 3333, '[taluk_name]'=> 'abas', '[Address]'=> 'bngl', '[PhoneNo]'=> 98586598596 ); //print_r($jsondata); //echo implode(" ",$formdata)."
"; $returned_content = get_data('http://192.168.1.181/Omps/Views/Mpcs/Services/CreateTaluk/',$formdata); echo "",print_r($returned_content,true),""; } } ?>

adarshdp commented Nov 2, 2017

my rest api is not accepting data what i m sending help me out

here code---

"50", "DistrictId" => "26123","taluk_id" => "26103", "taluk_name" => "kattreguppe","Address" => "bangluru", "PhoneNo" => "963258788"); function LogError($error) { $file = fopen("C:/xampp/htdocs/unittest/log/taluk.txt","a"); $t=time(); $t1=date("d-M-Y H:i:s: ", $t); fwrite($file,$t1); fwrite($file,$error); fwrite($file,"\r\n"); fclose($file); } function get_data($url,$returndata) { // $arry= implode(":",($returndata)); //curl_getinfo($ch, CURLINFO_HTTP_CODE); //$data=http_build_query($returndata); $data_json = json_encode($returndata,JSON_PRETTY_PRINT); //$data_json = json_encode(array("customer"=>$data)); LogError($data_json); //var_dump($data_json); //echo $data_json; //$ch = curl_init('http://api.local/rest/users'); /* $json_output = json_encode($returndata, JSON_UNESCAPED_SLASHES); $url = 'http://192.168.1.181/Omps/Views/Mpcs/Services/CreateTaluk/='.urlencode($json_output)'; */ $dat1='{"UnionId":55,"DistrictId":4521,"taluk_id":5252,"taluk_name":adars,"Address":bgnlr,"PhoneNo":9664445}'; $dat="UnionId=55&DistrictId=4521&taluk_id=5252&taluk_name=adars&Address=bgnlr&PhoneNo=9664445"; /* $postData = '';    //create name value pairs seperated by &    foreach($returndata as $key => $value)    {       $postData .= $key . '='.$value.'&';    }    $postData = rtrim($postData, '&'); */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_HTTPHEADER,array ("Accept: application/json",'Content-Type: application/json','Content-Length: ' . strlen($data_json))); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); //$data_json = urlencode(json_encode($returndata)); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //curl_setopt($ch, CURLOPT_POSTFIELDS,array("customer"=>$data_json)); //curl_setopt($ch, curl_setopt_array,$data_json); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; } // $answer="-*- MPCS Name: ".$UnionId ." MPCS ID: ". $DistrictId." Shift: ".$taluk_id; /* $formdata = array( 'UnionId'=> 1, 'DistrictId'=> 1111, 'taluk_id'=> 3333, 'taluk_name'=> 'abas', 'Address'=> 'bngl', 'PhoneNo'=> 98586598596 ); */ /*$formdata = Array ('UnionId' => 3,'DistrictId' => 260532,'taluk_id' => 26014232,'taluk_name' => 'dvg42','Address' => 'banglore42','PhoneNo' => 96327117652);*/ $formdata = Array ( '[UnionId]' => 3, '[DistrictId]' => 260532, '[taluk_id]'=> 3333, '[taluk_name]'=> 'abas', '[Address]'=> 'bngl', '[PhoneNo]'=> 98586598596 ); //print_r($jsondata); //echo implode(" ",$formdata)."
"; $returned_content = get_data('http://192.168.1.181/Omps/Views/Mpcs/Services/CreateTaluk/',$formdata); echo "",print_r($returned_content,true),""; } } ?>
@adarshdp

This comment has been minimized.

Show comment
Hide comment
@adarshdp

adarshdp Nov 2, 2017

failed to update

adarshdp commented Nov 2, 2017

failed to update

@cichondev

This comment has been minimized.

Show comment
Hide comment

Thanks!

@SakaSaiTrinath

This comment has been minimized.

Show comment
Hide comment
@SakaSaiTrinath

SakaSaiTrinath Nov 22, 2017

Excuse me I am getting the following error, syntaxerror: unexpected token # in json at position 0 at object.parse (native)

I have used the same example for testing and I got this error. Please help me with this.

Excuse me I am getting the following error, syntaxerror: unexpected token # in json at position 0 at object.parse (native)

I have used the same example for testing and I got this error. Please help me with this.

@Jayhello

This comment has been minimized.

Show comment
Hide comment

thx!!!!

@jaeycyril

This comment has been minimized.

Show comment
Hide comment
@jaeycyril

jaeycyril Jan 15, 2018

Thanks for this!

Thanks for this!

@hazratgs

This comment has been minimized.

Show comment
Hide comment

hazratgs commented Feb 2, 2018

Thanks!

@johnmahugu

This comment has been minimized.

Show comment
Hide comment
@johnmahugu

johnmahugu Feb 6, 2018

how can i do that in PHP?

how can i do that in PHP?

@Anilgarapati

This comment has been minimized.

Show comment
Hide comment
@Anilgarapati

Anilgarapati Feb 15, 2018

I need curl syntax for uploading a log file
ex:abc.log

I need curl syntax for uploading a log file
ex:abc.log

@mgiagante

This comment has been minimized.

Show comment
Hide comment
@mgiagante

mgiagante Mar 7, 2018

@pushyamig You can wrap all of your json objects in a single JSON array like so: [ {event1}, {event2}. {even3}] in case your API can accept them that way. If your API endpoint can only accept one at a single time, then you can write a bash script that traverses your json array and throws a curl command for each one of them. Other languages besides bash are more "pretty" for doing this task, like Ruby or Python.

@pushyamig You can wrap all of your json objects in a single JSON array like so: [ {event1}, {event2}. {even3}] in case your API can accept them that way. If your API endpoint can only accept one at a single time, then you can write a bash script that traverses your json array and throws a curl command for each one of them. Other languages besides bash are more "pretty" for doing this task, like Ruby or Python.

@mgiagante

This comment has been minimized.

Show comment
Hide comment
@mgiagante

mgiagante Mar 7, 2018

@Vanshita You probably need to escape your double quotes inside the JSON string with a \ character, so that your shell can know you're using them for the JSON string, and not for your command in the shell (which would close the parameter structure).
Also, notice that you have -XPOST . Try with -X POST.

mgiagante commented Mar 7, 2018

@Vanshita You probably need to escape your double quotes inside the JSON string with a \ character, so that your shell can know you're using them for the JSON string, and not for your command in the shell (which would close the parameter structure).
Also, notice that you have -XPOST . Try with -X POST.

@mgiagante

This comment has been minimized.

Show comment
Hide comment
@mgiagante

mgiagante Mar 7, 2018

@johnmahugu PHP has a curl extension, that allows you to use curl from inside PHP. Not sure if it's the best option, since I'm not a PHP guy https://williamjxj.wordpress.com/2010/11/23/php-curl-library/ .

@johnmahugu PHP has a curl extension, that allows you to use curl from inside PHP. Not sure if it's the best option, since I'm not a PHP guy https://williamjxj.wordpress.com/2010/11/23/php-curl-library/ .

@wellington1993

This comment has been minimized.

Show comment
Hide comment

Thanks!

@flyyabelle

This comment has been minimized.

Show comment
Hide comment

Thanks!

@Gyumeijie

This comment has been minimized.

Show comment
Hide comment

Thanks!

@Mr648

This comment has been minimized.

Show comment
Hide comment
@Mr648

Mr648 Jun 26, 2018

Thanks a lot!

Mr648 commented Jun 26, 2018

Thanks a lot!

@javieitez

This comment has been minimized.

Show comment
Hide comment
@javieitez

javieitez Jun 27, 2018

Really useful. Thank you!

Really useful. Thank you!

@ir2pid

This comment has been minimized.

Show comment
Hide comment
@ir2pid

ir2pid Jul 17, 2018

how do I post a map as form data

my script:

curl --user ${CIRCLE_TOKEN}: \
    --request POST \
    --form revision=xxx\
    --form config=@config.yml \
    --form notify=false \
    --form build_parameters={key=value} \
        https://circleci.com/api/v1.1/project/bitbucket/project/repo/tree/master

response:

{
"message" : "Invalid build_parameters value: must be a map"
}

ir2pid commented Jul 17, 2018

how do I post a map as form data

my script:

curl --user ${CIRCLE_TOKEN}: \
    --request POST \
    --form revision=xxx\
    --form config=@config.yml \
    --form notify=false \
    --form build_parameters={key=value} \
        https://circleci.com/api/v1.1/project/bitbucket/project/repo/tree/master

response:

{
"message" : "Invalid build_parameters value: must be a map"
}

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