尝试将变量作为URL的一部分传递时,出现CURL错误“ URL使用错误/非法格式的URL或缺少URL”

时间:2019-09-23 09:28:41

标签: bash curl jq

当我尝试执行以下脚本并出现错误时:“卷曲:(3)URL使用错误/非法格式或缺少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?

1 个答案:

答案 0 :(得分:0)

您是否需要拨打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