Sending nested JSON object using HTTPie

本小妞迷上赌 提交于 2019-11-27 10:27:43

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/37215565/sending-nested-json-object-using-httpie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!