bash 用多行 JSON 卷曲
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34847981/
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
Curl with multiline of JSON
提问by Ryan
Consider the curl command below, is it possible to allow newline in JSON (without the minify) and execute directly in bash (Mac/Ubuntu)
考虑下面的 curl 命令,是否可以在 JSON 中允许换行(没有 minify)并直接在 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"
}
}'
When I run the command above, seems error occurred at the second {
How to fix the above command?
当我运行上面的命令时,在second {
如何修复上面的命令?
Updated: actually I was able to run the command without issue previously, not sure why problem happen recently.
更新:实际上我以前可以毫无问题地运行命令,不知道为什么最近会出现问题。
回答by Eric Bolinger
I remembered another way to do this with a "Here Document" as described in the Bash man page and detailed here. The @-
means to read the body from STDIN, while << EOF
means to pipe the script content until "EOF" as STDIN to curl. This layout may be easier to read than using separate files or the "echo a variable" approach.
我记得使用 Bash 手册页中描述的“此处文档”执行此操作的另一种方法并在此处详细说明。从标准@-
输入读取正文的方法,同时<< EOF
意味着将脚本内容通过管道传输到“EOF”作为标准输入以卷曲。这种布局可能比使用单独的文件或“echo a variable”方法更容易阅读。
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
NOTE: Use the --trace <outfile>
curl option to record exactlywhat goes over the wire. For some reason, this Here Document approach strips newlines. (Update: Newlines were stripped by curl -d option. Corrected!)
注意:使用--trace <outfile>
curl 选项准确记录通过电线的内容。出于某种原因,这种 Here Document 方法去除了换行符。(更新:换行符被 curl -d 选项剥离。更正!)
回答by Bampfer
Along the lines of Martin's suggestion of putting the JSON in a variable, you could also put the JSON in a separate file, and then supply the filename to -d
using curl's @ syntax:
按照 Martin 将 JSON 放入变量的建议,您也可以将 JSON 放入单独的文件中,然后-d
使用 curl 的 @ 语法提供文件名:
curl -0 -v -X POST http://www.example.com/api/users \
-H "Expect:" \
-H 'Content-Type: text/json; charset=utf-8' \
-d @myfile.json
The disadvantage is obvious (2 or more files where you used to have one.) But on the plus side, your script could accept a filename or directory argument and you'd never need to edit it, just run it on different JSON files. Whether that's useful depends on what you are trying to accomplish.
缺点是显而易见的(2 个或更多文件,而您曾经有一个文件。)但从好的方面来说,您的脚本可以接受文件名或目录参数,而且您永远不需要编辑它,只需在不同的 JSON 文件上运行它。这是否有用取决于您要实现的目标。
回答by Dmitriy Korobkov
You should use outer double quotes, and the escape all inner quotes like this:
您应该使用外部双引号,并像这样转义所有内部引号:
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\"
}
}"
回答by Martin Konecny
You could assign your json to a var:
您可以将您的 json 分配给一个 var:
json='
{
"field1": "test",
"field2": {
"foo": "bar"
}
}'
Now you can forward this to curl using stdin
:
现在您可以使用stdin
以下命令将其转发给 curl :
echo $json | curl -0 -v -X POST http://www.example.com/api/users \
-H "Expect:" \
-H 'Content-Type: text/json; charset=utf-8' \
-d @-
回答by Tim Gebhardt
For some reason, this Here Document approach strips newlines
出于某种原因,这个 Here Document 方法去掉了换行符
@eric-bolinger the reason the Heredoc strips newlines is because you need to tell your Heredoc to preserve newlines by quoting the EOF:
@eric-bolinger Heredoc 去除换行符的原因是因为您需要通过引用 EOF 来告诉您的 Heredoc 保留换行符:
curl -0 -v -X POST http://www.example.com/api/users \
-H "Expect:" \
-H 'Content-Type: text/json; charset=utf-8' \
-d @- <<'EOF'
{
"field1": "test",
"field2": {
"foo": "bar"
}
}
EOF
Notice the single-ticks surrounding EOF the first time it's defined, but not the second.
注意第一次定义 EOF 时围绕 EOF 的单勾,但第二次没有。