Search code examples
bashshellcurlsh

Exception while making a POST using curl command in bash


I am working on a bash shell script that makes a POST API call to github to update a pull request status through curl command, below is the script I am using

export scan_status=''
export status_description=''
if[ "$(param.status)" == "Succeeded" ]; then
      scan_status="success"
      status_description="scan successful"
elif[ "$(param.status)" == "Failed" ]; then
      scan_status="failure"
      status_description="scan failed"
else
     scan_status="pending"
     status_description="scan pending"
fi

_HTTP_STATUS=$(
            curl --silent --show-error --insecure \
                --connect-timeout 20 --retry 5 --retry-delay 0 --retry-max-time 60 \
                --header 'Accept: application/vnd.github.v3+json' \
                --header 'Content-Type: application/json' \
                --header 'User-Agent: TektonCD, the peaceful cat' \
                --header 'Authorization: token '${GITHUB_TOKEN} \
                --output ${_OUT_DATA} \
                --write-out "%{http_code}\n" \
                --data '{"state":"${scan_status}","target_url":"","description":"${status_description}","context":"Checkmarx/$(params.datasource)"}' \
                --request POST \
                --url "$PR_STATUS_URL" | head -1
        )

        echo "status:${_HTTP_STATUS}"

When I hit this through script I am getting 422 error on debugging it, I noticed that the parameters scan_status,status_description in data section are being passed as "${scan_status}" & "${status_description}" rather their values.

I have tried passing them without double quotes, wrapping in single quotes, without curly brackets but none of them worked for me.

Can someone please help me to fix this


Solution

  • Update this line

    --data '{"state":"${scan_status}","target_url":"","description":"${status_description}","context":"Checkmarx/$(params.datasource)"}' \
    

    to

    --data "{\"state\":\"${scan_status}\",\"target_url\":\"\",\"description\":\"${status_description}\",\"context\":\"Checkmarx/$(params.datasource)\"}" \
    

    Note:

    • Values under single quotes doesn't get resolved in bash.