使用 HTTPie 发送嵌套的 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37215565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:25:39  来源:igfitidea点击:

Sending nested JSON object using HTTPie

jsonhttpie

提问by MaatDeamon

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

我正在尝试使用 HTTPie 来解析发送一些嵌套的 JSON 对象,但我找不到方法。很清楚如何发送 JSON 对象而不是嵌套对象,例如

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

{ “用户”:{ “姓名”:“约翰” “年龄”:10 } }

回答by Jakub Roztocil

You can pass the whole JSON via stdin:

您可以通过以下stdin方式传递整个 JSON

$ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post

Or specify the raw JSON as value with :=:

或者使用以下命令将原始 JSON 指定为值:=

$ http httpbin.org/post user:='{"name": "john", "age": 10 }'

回答by Nick Linker

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+Rin bash:

更可取,因为它与相关命令具有相同的前缀,因此Ctrl+R在 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:

如果您有fishshell,但没有Here Strings,我可以提出以下解决方法:

~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end
~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}')