Bash:在命令行工作,但在脚本中使用时得到 'curl: (1) Protocol ""https" not supported or disabled in libcurl'

Bash: works at command line but getting 'curl: (1) Protocol ""https" not supported or disabled in libcurl' when using in a script

遇到以下情况:curl 请求可以在命令行中正常工作,但在脚本中无法正常工作,会出现奇怪的错误。这是脚本的一部分:

#we already got the $token
#vars defining curl parameters
dc="NA EU AU SEA JP"
curl_url='https://example.com/api/config/v1/'
curl_request='alertingProfiles?'
curl_properties='tenant=all&stage=prd&cluster='
curl_auth='"Authorization: Bearer '"$token"'"'
for k in $dc; do

        # Download the json for each DC using the curl request
        curl_combined=\""$curl_url$curl_request$curl_properties$k"\"; echo "$curl_combined"
        curlCMD=( -vv --location --request GET "$curl_combined"  -H "$curl_auth" -o \""$k.json"\")
        echo "${curlCMD[@]}"
        curl "${curlCMD[@]}"
        done

当我在脚本中 运行 时,我看到以下输出: 首先,第一个 echo 命令的输出

"https://example.com/api/config/v1/alertingProfiles?tenant=all&stage=prd&cluster=NA"

然后,curl 应该作为参数的第二个 echo 命令的输出,我已经跳过了不记名令牌,但是相信我,这不是问题。

-vv --location --request GET "https://example.com/api/config/v1/alertingProfiles?tenant=all&stage=prd&cluster=NA" -H "Authorization: Bearer " -o "NA.json"

我得到以下输出

Note: Unnecessary use of -X or --request, GET is already inferred.
* Protocol ""https" not supported or disabled in libcurl
* Closing connection -1
curl: (1) Protocol ""https" not supported or disabled in libcurl

请注意上面消息中 https 前面的两个双引号

如果我只是 copy/paste 在 curl 和执行之后立即在命令行中输出上面的第二个 echo 输出,它会按预期工作并在文件中下载 JSON。

如果我从 curl_combined var 中删除 ",则发送的请求不带双引号,现在我从脚本中收到消息 curl: (56) Unexpected EOF

如果我 copy/paste 第二个 echo 输出(没有双引号),将它提供给 curl,并从命令行执行,我会收到 403 消息,直到我在 https 请求周围加上双引号或单引号.

简而言之 - 似乎有必要在 https 请求周围加上双引号,但出于某种原因,curl 不接受它。

所以这里出了什么问题?我什至不介意将构造的 URL 写入文件并从中读取或使用 herestring(如果有帮助),但想知道这里到底出了什么问题。

谢谢。

P.S。删除 --location--request GET 或两者都不会改变结果。

脚本使用 "${curlCMD[@]}" 数组作为参数传递给 curl。这种方法将处理 url 的正确扩展,而不管其中的任何特殊字符。无需使用 \" 结构将 curl_combined 和额外的引号括起来。

建议将设置 curl_combined 替换为:

curl_combined="$curl_url$curl_request$curl_properties$k"
echo "$curl_combined"

此解决方案可以将 curl 请求替换为以下数组

curl -vv \
--location \
--request GET \
"$curl_combined" \
--header "Authorization: Bearer ${token}" \
-o "$k.json"

不过很丑,还是客气点说吧...