将 Bash 变量传递给 CURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42813926/
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-18 15:53:53  来源:igfitidea点击:

Passing Bash Variable to CURL

bashshellcurl

提问by Vikram

when i give the values like this it works :

当我给出这样的值时,它会起作用:

curl --silent \ --insecure \ -X POST \ -d '{"Name" : "Vikram"}' \ -H "Content-Type: application/json" \ $restUrl

but when i give it like this :

但是当我这样给它时:

post="'{\"Name\" : \"Vikram\"}'"    
echo $post   // Prints '{"Name" : "Vikram"}'
echo ${post} // Prints '{"Name" : "Vikram"}'

but the following does not work and throws an 400 error:

但以下不起作用并引发 400 错误:

curl --silent \ --insecure \ -X POST \ -d ${post} \ -H "Content-Type: application/json" \ $restUrl

回答by twm

It seems there are multiple issues.

似乎有多个问题。

1) To address the question asked by your title, when you use $post as an argument, the white space in its value causes it to be treated as multiple arguments by Bash. Try putting quotes around it so that it's treated as one argument.

1)为了解决您的标题提出的问题,当您使用 $post 作为参数时,其值中的空格会导致 Bash 将其视为多个参数。尝试在它周围加上引号,以便将其视为一个参数。

2) I'm guessing you added single quotes to $post, in an attempt to have Bash treat it as a single parameter. Try removing the single quotes.

2) 我猜你在 $post 中添加了单引号,试图让 Bash 将它视为单个参数。尝试删除单引号。

3) For me at least, all of the backslashes in the curlcommand were causing it to fail. Maybe you had it split across multiple lines and the copy/paste didn't translate that. I've removed them in my example below.

3)至少对我来说,curl命令中的所有反斜杠都导致它失败。也许你把它分成多行,复制/粘贴没有翻译。我在下面的示例中删除了它们。

Putting all together:

放在一起:

post="{\"Name\" : \"Vikram\"}"
curl --silent --insecure -X POST -d "${post}" -H "Content-Type: application/json" $restUrl

回答by Barmar

Don't put single quotes inside the $postvariable, put them around the value to quote it. Then you don't need to escape the double quotes inside the value.

不要将单引号放在$post变量内,将它们放在值周围以引用它。然后你不需要转义值内的双引号。

post='{"Name" : "Vikram"}'

Then quote the variable when you use it in the curlcommand line, to prevent it from being split into multiple arguments.

然后在curl命令行中使用变量时引用该变量,以防止将其拆分为多个参数。

You also shouldn't have all those backslashes in the curlcommand line. They're escaping the space after them, so they'll be treated as literal arguments rather than delimiters. The usual time to use backslash in a command is if you're splitting it across multiple lines, then you need to escape the newline so it doesn't end the command.

您也不应该在curl命令行中包含所有这些反斜杠。它们在它们之后转义空格,因此它们将被视为文字参数而不是分隔符。在命令中使用反斜杠的通常时间是,如果您将它拆分为多行,那么您需要对换行符进行转义,这样它就不会结束命令。

curl --silent --insecure -X POST -d "${post}" -H "Content-Type: application/json" "$restUrl"

回答by jraynal

You want to quote ${post}: "${post}", so that your shell does not split the content of your variable.

您想引用${post}: "${post}",以便您的 shell 不会拆分变量的内容。