Sending nested JSON object using HTTPie

JsonHttpie

Json Problem Overview


I am trying to use HTTPie to parse to send some nested JSON object, but I can not find how. It is pretty clear how to send a JSON object but not a nested one such as

> { "user": { "name": "john" > "age": 10 } }

Json Solutions


Solution 1 - 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 }'

Solution 2 - Json

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 }}')

Solution 3 - Json

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

Solution 4 - Json

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaatDeamonView Question on Stackoverflow
Solution 1 - JsonJakub RoztocilView Answer on Stackoverflow
Solution 2 - JsonNick LinkerView Answer on Stackoverflow
Solution 3 - JsonZach ValentaView Answer on Stackoverflow
Solution 4 - JsonAndrewView Answer on Stackoverflow