Sending nested JSON object using HTTPie

2019-02-02 21:56发布

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 httpie
2条回答
该账号已被封号
2楼-- · 2019-02-02 22:26

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 }'
查看更多
别忘想泡老子
3楼-- · 2019-02-02 22:29

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 }}')
查看更多
登录 后发表回答