gpt4 book ai didi

bash - 使用多行 JSON 进行 curl

转载 作者:行者123 更新时间:2023-11-29 08:40:08 25 4
gpt4 key购买 nike

考虑下面的 curl 命令,是否可以在 JSON 中允许换行(没有缩小)并直接在 bash 中执行(Mac/Ubuntu)

curl -0 -v -X POST http://www.example.com/api/users \
-H "Expect:" \
-H 'Content-Type: text/json; charset=utf-8' \
-d \
'
{
"field1": "test",
"field2": {
"foo": "bar"
}
}'

当我运行上面的命令时,似乎在 second { 发生了错误如何修复上述命令?

已更新:实际上我以前可以毫无问题地运行命令,不确定为什么最近会出现问题。

最佳答案

我记得 Bash 手册页和 detailed here 中描述的另一种使用“此处文档”执行此操作的方法. @-表示从 STDIN 读取正文,而 << EOF意味着将脚本内容通过管道传递到“EOF”作为 STDIN 进行 curl 。这种布局可能比使用单独的文件或“回显变量”方法更容易阅读。

curl -0 -v -X POST http://www.example.com/api/users \
-H "Expect:" \
-H 'Content-Type: application/json; charset=utf-8' \
--data-binary @- << EOF
{
"field1": "test",
"field2": {
"foo": "bar"
}
}
EOF

注意:使用 --trace <outfile> curl 选项可以准确地记录通过网络传输的内容。出于某种原因,这种 Here Document 方法会去除换行符。 (更新:换行符被 curl -d 选项删除。更正!)

关于bash - 使用多行 JSON 进行 curl ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com