Sending nested JSON object using HTTPie
On Windows 10 (cmd.exe) the syntax is a little bit different due to quoting rules. Properties/strings need to be surrounded by double quotes.
http -v post https://postman-echo.com/post address:="{""city"":""london""}"
POST /post HTTP/1.1
Content-Type: application/json
Host: postman-echo.com
User-Agent: HTTPie/2.3.0
{
"address": {
"city": "london"
}
}
You can also send the whole object using echo, and without double quoting.
echo {"address": {"city":"london"} } | http -v post https://postman-echo.com/post
Another approach mentioned in the httpie docs is using a JSON file; this has worked well for me for payloads that are more verbose and deeply nested.
http POST httpbin.org/post < post.json
Update for HTTPie 3.0 released in January 2022:
There’s now built-in support for nested JSON using the HTTPie language:
$ http pie.dev/post \
tool[name]=HTTPie \
tool[about][homepage]=httpie.io \
tool[about][mission]='Make APIs simple and intuitive' \
tool[platforms][]=terminal \
tool[platforms][]=desktop \
tool[platforms][]=web \
tool[platforms][]=mobile
{
"tool": {
"name": "HTTPie",
"about": {
"mission": "Make APIs simple and intuitive"
"homepage": "httpie.io",
},
"platforms": [
"terminal",
"desktop",
"web",
"mobile",
]
}
}
You can learn more about nested JSON in the docs: https://httpie.io/docs/cli/nested-json
Old answer for HTTPie older than 3.0:
You can pass the whole JSON via stdin
:
$ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post
Or specify the raw JSON as value with :=
:
$ http httpbin.org/post user:='{"name": "john", "age": 10 }'
I like this way:
$ http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}'
It is preferrable because it has the same prefix as the related commands, and so it is convenient to find the commands with Ctrl+R
in bash:
$ http localhost:8080/user/all
$ http GET localhost:8080/user/all # the same as the previous
$ http DELETE localhost:8080/user/234
If you have fishshell
, which doesn't have Here Strings, I can propose the following workaround:
~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end
~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}')