Home > Software engineering >  CURL variables and problems parsing JSON
CURL variables and problems parsing JSON

Time:12-16

My script is creating pull requests using the 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

How can I set my variable into the curl correctly?

CodePudding user response:

$Branch needs to be quoted as well:

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