httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.9k stars 3.68k forks source link

Syntax for nested JSON #78

Closed sym3tri closed 2 years ago

sym3tri commented 12 years ago

The simple examples work, but what about nested data? In curl I do something like this:

-d \ '"credentials": { "username": "me", "key": "my-key"} }'

How can I do this with httpie?

jkbrzt commented 12 years ago

For deeply nested JSON it's easier to use stdin. For example:

 echo '{"foo": {"bar": "baz"}}' | http url

 # Or from a file:
 http url < file.json

More examples are listed in the README.

sandstrom commented 9 years ago

httpie is a dream! nesting would make it even more awesome though.

It would be nice with rails-like nesting, e.g. path/to/endpoint credentials[username]=me

mfussenegger commented 9 years ago

Some sort of easier syntax for nested data would really be a nice thing to have. There is also another issue with some syntax suggestions: https://github.com/jakubroztocil/httpie/issues/346

jkbrzt commented 9 years ago

So nesting could be expressed either with brackets or using dot notation.

akamalov commented 9 years ago

Greetings!!! Small question regarding nested json and httpie. So I have a json file:

{
      "apps": [
         { "id": "/product/web/tutum-hello-world-2",
                   "container": {
               "type": "DOCKER",
                   "docker": {
                     "image": "tutum/hello-world",
                         "network": "BRIDGE"
                          }
                        },
                        "id": "/product/web/tutum-hello-world-2",
                        "instances": 1,
                        "cpus": 0.25,
                        "mem": 256,
                        "uris": [],
                        "constraints": [["hostname", "LIKE", "server1.localnet.net"]]

                }
        ]
}

I want to increase the value of "instances": to 2 and use "constraints": [["hostname", "LIKE", "server1.localnet.net"]]. I tried different permutations, but I am getting No JSON object could be decoded over and over again :(

Using just instances: is pretty easy:

root@node108:~# http -v PUT http://10.10.10.219:8080/v2/apps/product/web/tutum-hello-world-1 instances:=1
PUT /v2/apps/product/web/tutum-hello-world-1 HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 16
Content-Type: application/json; charset=utf-8
Host: 10.9.158.219:8080
User-Agent: HTTPie/0.8.0

{
    "instances": 1
}

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 92
Content-Type: application/json
Expires: 0
Pragma: no-cache
Server: Jetty(8.y.z-SNAPSHOT)

{
    "deploymentId": "6eeda6ac-2ec1-4554-95f3-a0dc19333c56", 
    "version": "2015-06-10T11:33:53.165Z"
}

...however using it TOGETHER with constraints directive + nested values has proven to be difficult (for me). Any help, please ???

Thanks!!!!

akamalov commented 9 years ago

Answering my own question:

http -v PUT http://10.10.10.219:8080/v2/apps/product/web/tutum-hello-world-1 instances:=2 constraints:='[["hostname", "LIKE", server1.localnet.nett"]]'

PUT /v2/apps/product/web/tutum-hello-world-1 HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 85
Content-Type: application/json; charset=utf-8
Host: 10.9.158.219:8080
User-Agent: HTTPie/0.8.0

{
    "constraints": [
        [
            "hostname", 
            "LIKE", 
            "server1.localnet.net"
        ]
    ], 
    "instances": 2
}

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 92
Content-Type: application/json
Expires: 0
Pragma: no-cache
Server: Jetty(8.y.z-SNAPSHOT)

{
    "deploymentId": "bde62f56-89f6-406a-9c3a-d0309ea3dde6", 
    "version": "2015-06-10T12:28:06.056Z"
}

Thanks!!

Alex

jkbrzt commented 8 years ago

Syntax: https://www.w3.org/TR/html-json-forms/

(https://github.com/jdp/jarg)

lastcoolnameleft commented 8 years ago

@jkbrzt For clarification are you referencing jarg for your own notes/feature development? I tried the following which didn't work:

http post localhost:3000/api foo[bar]=baz

Is received as:

body= { 'foo[bar]': 'baz' }

However, piping through jarg does seem to work:

jarg foo[bar]=baz | http post localhost:3000/api

Is received as:

body= { foo: { bar: 'baz' } }
jkbrzt commented 8 years ago

@lastcoolnameleft yes, I was just bookmarking that spec. Sorry for the confusion.

Maatary commented 8 years ago

Hi, I am not sure to understand, how do you provided nested json object with Httpie

akvadrako commented 8 years ago

I would prefer javascript compatible syntax, so http post parent.child=value and the more complex syntax for embedded dots: [parent.with.dots]=value. The common use case should be easiest and json properties with dots are ugly to use in javascript.

rubemz commented 6 years ago

any updates on this issue? I'd also prefer what @akvadrako suggested

odusseys commented 6 years ago

agree that this would be very welcome.

nikolay commented 6 years ago

@jakubroztocil This issue has been open for 6 years! And it is such a rudimentary feature! Of course, we can all do echo, but we can use curl as well, right? The point is that httpie does all the basic REST operations. Of course, we can use it along with jarg or jo, but it's so easy to implement this within httpie, I think!

merwok commented 6 years ago

I don’t think it’s that simple, or someone would have sent a pull request!

We should appreciate projects written on free time and given for free.

nikolay commented 6 years ago

@merwork We appreciate it. But how many REST services you know to have only a flat JSON?

merwok commented 6 years ago

Many that I write :)

Thanks for the pointers to jarg and jo! Goes well in my json toolbox with jq and gron.

segevfiner commented 4 years ago

If you don't like piping JSON from jo or jarg before the http command. you can also use shell process substitution so the JSON is in a similar location when using the builtin syntax of HTTPie:

https postman-echo.com/post @<(jo hello=world)

It would still be much better if HTTPie supported a syntax like those tools of course.

jkbrzt commented 3 years ago

An important consideration here is backwards compatibility. If we simply introduce a syntax relying on dots, than a script containing the following HTTPie command:

$ http httpbin.org/post  foo.bar=baz

Which currently produces this JSON:

{
    "foo.bar": "baz"
}

Would unexpectly result in:

{
    "foo": {
        "bar": "baz"
    }
}

So the command would have to be updated to escape the newly-meanignful . character:

$ http httpbin.org/post  'foo\.bar=baz'
merwok commented 3 years ago

+1 for the compat issue, but the proposed solution should be more complicated:

$ http httpbin.org/post "foo\.bar=baz"

otherwise the shell escape \. is interpreted by the shell as . and the backslash is not passed to the command called.

jkbrzt commented 3 years ago

@merwok Good point. I’ve updated the example to take shell escaping into account. General request item escaping rules apply.

BoboTiG commented 3 years ago

Note that since HTTPie 2.5.0, you can use --raw in the same way you do curl -d:

$ http pie.dev/post --raw '{"foo": {"bar": "baz"}}'
{
    "foo": {
        "bar": "baz"
    }
}
isidentical commented 2 years ago

🎉 This feature is now publicly available in HTTPie for Terminal 3.0:

👉🏻 https://httpie.io/docs/cli/nested-json