使用 HTTPie 发送嵌套的 JSON 对象
Sending nested JSON object using HTTPie
我正在尝试使用 HTTPie 来解析以发送一些嵌套的 JSON 对象,但我找不到具体方法。很清楚如何发送 JSON 对象而不是嵌套对象,例如
{ "user": { "name": "john"
"age": 10 } }
2022 年 1 月发布的 HTTPie 3.0 更新:
现在内置了对使用 HTTPie 语言的嵌套 JSON 的支持:
$ http pie.dev/post \
tool[name]=HTTPie \
tool[about][homepage]=httpie.io \
tool[about][mission]='Make APIs simple and intuitive' \
tool[platforms][]=terminal \
tool[platforms][]=desktop \
tool[platforms][]=web \
tool[platforms][]=mobile
{
"tool": {
"name": "HTTPie",
"about": {
"mission": "Make APIs simple and intuitive"
"homepage": "httpie.io",
},
"platforms": [
"terminal",
"desktop",
"web",
"mobile",
]
}
}
您可以在文档中了解有关嵌套 JSON 的更多信息:https://httpie.io/docs/cli/nested-json
HTTPie 3.0 之前的旧答案:
你可以pass the whole JSON via stdin
:
$ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post
或specify the raw JSON as value with :=
:
$ http httpbin.org/post user:='{"name": "john", "age": 10 }'
我喜欢这样:
$ http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}'
最好是和相关命令有相同的前缀,方便在bash中查找带Ctrl+R
的命令:
$ http localhost:8080/user/all
$ http GET localhost:8080/user/all # the same as the previous
$ http DELETE localhost:8080/user/234
如果您有 fishshell
, which doesn't have Here Strings,我可以提出以下解决方法:
~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end
~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}')
httpie 文档中提到的另一种方法是使用 JSON file;对于更冗长和深层嵌套的有效载荷,这对我来说效果很好。
http POST httpbin.org/post < post.json
在 Windows 10 (cmd.exe) 上,由于引用规则,语法有 一点点 的不同。 Properties/strings需要用双引号括起来。
http -v post https://postman-echo.com/post address:="{""city"":""london""}"
POST /post HTTP/1.1
Content-Type: application/json
Host: postman-echo.com
User-Agent: HTTPie/2.3.0
{
"address": {
"city": "london"
}
}
您也可以使用 echo 发送整个对象,而不用双引号。
echo {"address": {"city":"london"} } | http -v post https://postman-echo.com/post
我正在尝试使用 HTTPie 来解析以发送一些嵌套的 JSON 对象,但我找不到具体方法。很清楚如何发送 JSON 对象而不是嵌套对象,例如
{ "user": { "name": "john" "age": 10 } }
2022 年 1 月发布的 HTTPie 3.0 更新:
现在内置了对使用 HTTPie 语言的嵌套 JSON 的支持:
$ http pie.dev/post \
tool[name]=HTTPie \
tool[about][homepage]=httpie.io \
tool[about][mission]='Make APIs simple and intuitive' \
tool[platforms][]=terminal \
tool[platforms][]=desktop \
tool[platforms][]=web \
tool[platforms][]=mobile
{
"tool": {
"name": "HTTPie",
"about": {
"mission": "Make APIs simple and intuitive"
"homepage": "httpie.io",
},
"platforms": [
"terminal",
"desktop",
"web",
"mobile",
]
}
}
您可以在文档中了解有关嵌套 JSON 的更多信息:https://httpie.io/docs/cli/nested-json
HTTPie 3.0 之前的旧答案:
你可以pass the whole JSON via stdin
:
$ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post
或specify the raw JSON as value with :=
:
$ http httpbin.org/post user:='{"name": "john", "age": 10 }'
我喜欢这样:
$ http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}'
最好是和相关命令有相同的前缀,方便在bash中查找带Ctrl+R
的命令:
$ http localhost:8080/user/all
$ http GET localhost:8080/user/all # the same as the previous
$ http DELETE localhost:8080/user/234
如果您有 fishshell
, which doesn't have Here Strings,我可以提出以下解决方法:
~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end
~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}')
httpie 文档中提到的另一种方法是使用 JSON file;对于更冗长和深层嵌套的有效载荷,这对我来说效果很好。
http POST httpbin.org/post < post.json
在 Windows 10 (cmd.exe) 上,由于引用规则,语法有 一点点 的不同。 Properties/strings需要用双引号括起来。
http -v post https://postman-echo.com/post address:="{""city"":""london""}"
POST /post HTTP/1.1
Content-Type: application/json
Host: postman-echo.com
User-Agent: HTTPie/2.3.0
{
"address": {
"city": "london"
}
}
您也可以使用 echo 发送整个对象,而不用双引号。
echo {"address": {"city":"london"} } | http -v post https://postman-echo.com/post