gpt4 book ai didi

bash - 将 Bash 变量传递给 CURL

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

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

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

但是当我这样给它时:

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

但以下内容不起作用并抛出 400 错误:

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

最佳答案

似乎有多个问题。

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

2) 我猜你给 $post 添加了单引号,试图让 Bash 把它当作一个参数。尝试删除单引号。

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

放在一起:

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

关于bash - 将 Bash 变量传递给 CURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813926/

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