【问题标题】:CURL error "URL using bad/illegal format or missing URL" when trying to pass variable as a part of URL尝试将变量作为 URL 的一部分传递时出现 CURL 错误“URL 使用错误/非法格式或缺少 URL”
【发布时间】:2019-09-23 09:28:41
【问题描述】:

当我尝试执行以下脚本并收到错误消息时:“curl: (3) URL using bad/illegal format or missing URL”

#!/bin/bash

stage="develop"
branch="branch_name"

getDefinition=$(curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions?api-version=5.1")

for def in $(echo "$getDefinition" | jq '.value[] | select (.path=="\\Some_path\\'$stage'") | .id'); do
  getBuildInfo=$(curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1")
        # echo $def
        body=$(echo "${getBuildInfo}" | jq '.repository.defaultBranch = "refs/heads/release/'"${branch}"'"' | jq '.options[].inputs.branchFilters = "[\"+refs/heads/release/'"${branch}"'\"]"' | jq '.triggers[].branchFilters[] = "+refs/heads/release/'"${branch}"'"')
        echo ${body} > data.json    
done

当我尝试将变量 ${def} 传递到一行时会发生这种情况:

curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"

但是当我声明一个数组时,curl 按预期工作。 示例:

declare -a def
def=(1 2 3 4)

curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"

您能否建议我如何正确地将变量传递到 URL 中?

【问题讨论】:

  • 尝试 echo "-"$def"-" 看看是否有不需要的前导或结尾字符
  • 这可能不会显示非打印字符,echo 可能会将“-”误认为是选项分隔符。我会推荐echo "'$def'" | LC_ALL=C cat -vt。或者在问题部分之前使用set -x,这样shell 会在执行命令时打印命令(可能会在任何有趣的字符周围加上奇怪的引用/转义)。
  • 非常感谢@franzisk,@GordonDavisson!变量 $def 包含 ^M 字符。删除此 CURL 后工作正常。

标签: bash curl jq


【解决方案1】:

你需要调用 curl 4 次吗?如果是这样。

for def in 1 2 3 4; do curl -u user@example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"; done

【讨论】:

  • 问题已解决。变量包含 ^M 字符。删除此 CURL 后工作正常。
猜你喜欢
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多