cURL 转义和数据中

问题描述 投票:0回答:1

我编写了一个脚本来与 GitLab CI 一起运行并更新徽章,但我没有得到正确的结果

这是功能:

vuejs_version() {

    vuejs_version='x.y.z'

    vuejs_badge_id=$(curl --header "PRIVATE-TOKEN:$BADGE_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges" | jq -c 'map(select(.name | contains("vuejs")))[0].id')

    response=$(curl --request PUT --header "PRIVATE-TOKEN:$BADGE_TOKEN" \
       --data "image_url=https://img.shields.io/static/v1?label=VueJs&message=$vuejs_version&color=orange" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges/$vuejs_badge_id")

    echo "cURL response: $response"

}

比我的回复:

{
  "name":"vuejs",
  "link_url":"https://gitlab.com/%{project_path}/-/commits/%{default_branch}",
  "image_url":"https://img.shields.io/static/v1? label=VueJs",
  ...
}

作为响应,image_url缺少参数:

“image_url”:“https://img.shields.io/static/v1?label=VueJs”

但应该是这样的:

'https://img.shields.io/static/v1?label=VueJs&message=x.y.z&color=red'

bash shell curl escaping ampersand
1个回答
1
投票

正如R先生回答的那样,--data-urlencode就像一个魅力;

另外,我创建了一个包含数据和 url 字符串的变量:

vuejs_version() {

    vuejs_version='x.y.z'

    vuejs_badge_id=$(curl --header "PRIVATE-TOKEN:$BADGE_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges" | jq -c 'map(select(.name | contains("vuejs")))[0].id')

    data=$(echo "image_url=https://img.shields.io/static/v1?label=VueJs&message=${vuejs_version}&color=red" | sed -e "s/ //g")
    url=$(echo "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges/$vuejs_badge_id")

    response=$(curl --request PUT --header "PRIVATE-TOKEN:$BADGE_TOKEN" \
        --data-urlencode ${data} ${url})

    echo "cURL response: $response"

}
© www.soinside.com 2019 - 2024. All rights reserved.