Closed sym3tri closed 2 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.
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
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
So nesting could be expressed either with brackets or using dot notation.
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!!!!
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 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' } }
@lastcoolnameleft yes, I was just bookmarking that spec. Sorry for the confusion.
Hi, I am not sure to understand, how do you provided nested json object with Httpie
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.
any updates on this issue? I'd also prefer what @akvadrako suggested
agree that this would be very welcome.
@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!
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.
@merwork We appreciate it. But how many REST services you know to have only a flat JSON?
Many that I write :)
Thanks for the pointers to jarg and jo! Goes well in my json toolbox with jq and gron.
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.
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'
+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.
@merwok Good point. I’ve updated the example to take shell escaping into account. General request item escaping rules apply.
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"
}
}
🎉 This feature is now publicly available in HTTPie for Terminal 3.0:
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?