【问题标题】:CURL variables and problems parsing JSON [duplicate]CURL 变量和解析 JSON 的问题 [重复]
【发布时间】:2022-01-19 05:36:14
【问题描述】:

我的脚本正在使用 github API 创建拉取请求:

./my-script.sh my-repo my-branch

#!/bin/bash

Repo=$1
Branch=$2

cd $Repo
 
get_data() {
cat <<EOF
    {
        "title": "PR title",
        "head": $Branch,
        "base": "development",
        "body": "PR description"
    }
EOF
}

echo $(get_data) # <----------------- I can see my value of the variable $Branch here

curl -X POST \
    -H "Accept: application/vnd.github.v3+json" \
    -d "$(get_data)" \ # <----------------- But here I'm facing "Problems parsing JSON"
    -u my_user:my_token \
    https://api.github.com/repos/my_user/$Repo/pulls

open https://github.com/my_user/$Repo/pulls

如何正确地将我的变量设置为 curl?

【问题讨论】:

  • 函数 get_data 在没有变量的情况下按预期工作
  • 用 '? 定义数据`get_data() { cat
  • @Dan_Maff 你是对的。 "head": "$Branch",成功了!
  • @Dan_Maff 随意添加您的答案。我希望我的脚本对某人有用
  • 我没有尝试使用撇号。当我将变量用双引号括起来时它起作用了

标签: bash shell curl github-api


【解决方案1】:

$Branch 也需要引用:

get_data() {
  cat <<EOF
    {
        "title": "PR title",
        "head": "$Branch",
        "base": "development",
        "body": "PR description"
    }
  EOF
}

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 1970-01-01
    • 2012-11-12
    • 2018-03-12
    • 2015-06-02
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多