Skip to content

Instantly share code, notes, and snippets.

Last active April 19, 2023 12:09
What would you like to do?
post a JSON file with curl

How do you POST a JSON file with curl??

You can post a json file with curl like so:

curl -X POST -H "Content-Type: application/json" -d @FILENAME DESTINATION

so for example:

curl -X POST -H "Content-Type: application/json" -d @../data/cats.json http://localhost:8080/mSfvMwNAfj

you can post a little blob of geojson, like so:

curl -X POST -H "Content-Type: application/json" -d '{"type":"Feature","properties":{"COUNTY":"M","PRECINCT":4505.0,"AREA":24330472.48801},"geometry":{"type":"Polygon","coordinates":[[[-122.579029970781065,45.53373981165327],[-122.579036751674209,45.533742065313575],[-122.579532751335037,45.533906068395808],[-122.579961749295705,45.534067060396104],[-122.580146749064056,45.534114065074704],[-122.581902757566823,45.534772060883959],[-122.582770766094569,45.535128058282019],[-122.583461773870638,45.535383050711246],[-122.584275765662781,45.535596050402262],[-122.584926770675438,45.535732043153857],[-122.585534768605342,45.535823041352423],[-122.586393777046183,45.535863042628513],[-122.586845771939593,45.535841037753478],[-122.587166778711079,45.535850042847976],[-122.587593783117342,45.535829037919441],[-122.587900782381638,45.535804036638346],[-122.588290773918729,45.535750036812139],[-122.588704782064923,45.535671034778389],[-122.589196776268082,45.535561035548149],[-122.589239647326053,45.535550318867173],[-122.589320708749753,45.536124484404517],[-122.589304695035338,45.537268231315537],[-122.59947923271595,45.53740953002616],[-122.599372142997979,45.537656509885132],[-122.601520781310597,45.537617244011571],[-122.601391421360532,45.543541079913162],[-122.578671442407,45.552336859399695],[-122.578672106607101,45.55230042862145],[-122.578693794893084,45.551092987187559],[-122.578713188347777,45.549969468845575],[-122.579029970781065,45.53373981165327]]]}}' http://localhost:8080/mSfvMwNAfj

Don't try to post a big blob, your terminal will hate you and you will hate yourself. It will go on forever and ever.

Use the syntax above (curl -X POST -H "Content-Type: application/json" -d @FILENAME DESTINATION) to specify a file instead.

Copy link

@snepal87 sounds like you need to use a function.

good luck!

Copy link


Copy link

Thanks. Cured my headache.

Copy link

How to know my destination?

Copy link

Thank you very much 👍

Copy link

Fran-Rg commented Jul 14, 2021

How would you handle special character in the filename? For example spaces or '(' I've tried single quoting them:
--data '@xyz (2).json' but I got "unexpected token '('"

Copy link

@Fran-Rg I would guess you have to escape special characters, using \

Copy link

labuscpi commented Jul 16, 2021

What if I need to change a value of a Json object while posting?

For example my filename.json is

"id": "abcd",
"name": "name"
"address": "street",

My "id" may not always be "abcd" and it can be dynamic.
I want to Post this Json file using the above Curl but I want to pass "id" as dynamic value every time I Post the Json. How do I do this?




create_file () {

        echo "{\"id\":\"$1\",\"name\":\"name\",\"address\":\"street\"}"
    } >"${FILE_NAME}"


create_file "abcd"

curl -X POST -H "Content-Type: application/json" -d @"${FILE_NAME}" "${DESTINATION}"

Copy link

laduke commented Sep 28, 2021

I googled "curl post json file" and this is the #1 hit 🤙

Copy link

Many thanks!

Copy link

Gerst20051 commented Mar 3, 2022

You can cat the contents of a json file to curl via the --data-raw parameter

curl -H 'Content-Type: application/json' --data-raw "$(cat ~/.json/payload-2022-03-03.json | grep -v '^\s*//')"

curl -H 'Content-Type: application/json' -d @<(jq . ~/.json/payload-2022-03-03.json)

curl -H 'Content-Type: application/json' -d @<(jq '{"payload": .}' < ~/.json/payload-2022-03-03.json)

Note: comments in the json file are filtered out via grep -v '^\s*//'

You can also pass the data to curl via stdin using grep or cat or jq

grep -v '^\s*//' ~/.json/payload-2022-03-03.json | curl -H 'Content-Type: application/json' -d @-

cat ~/.json/payload-2022-03-03.json | grep -v '^\s*//' | curl -H 'Content-Type: application/json' -d @-

jq . ~/.json/payload-2022-03-03.json | curl -H 'Content-Type: application/json' -d @-

jq '{"payload": .}' < ~/.json/payload-2022-03-03.json | curl -H 'Content-Type: application/json' -d @-

link to my stackoverflow answer

Copy link

moyamos commented Jul 13, 2022

Is it possible to insert data from a file in an already existing JSON structure like this?

curl -X POST --data "{\"data\":@./test.json, \"id\": 10}" DESTINATION

Copy link

laduke commented Jul 13, 2022

Maybe run it through jq then pipe like in Gerst20051's previous reply

jq '{"data": .}' <./test.json | curl ...

i haven't tried it

Copy link

moyamos commented Jul 16, 2022

@laduke Thank you. It works!

Copy link

What if I want to post a file(csv) along with json data? Is that possible? If so how can I do that? Ex:

curl --location --request POST ''
--header 'Authorization: Bearer '
--header 'Content-Type: application/json'
--data-raw '{
"user": {"email":""},
"options": {"process":"order_lines","selection":"price","tolerance":"loose","target":"transport_orders"},
"import_file": "/path/to/file.csv"

I want to achieve something like this, how can I do this?

Copy link

@ehsanashar you want the JSON to contain the entire contents of the CSV under the import_file key? I'm not sure you can do that with cURL alone but it shouldn't be very dificult to glue a few shell commands together and achieve the same result.

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