Veerabbhadrayya Math
Veerabbhadrayya Math

Reputation: 45

How to insert another JSON, in the POST cURL request JSON data?

my report json looks like this:

{
  "passed": 11,
  "total": 11,
  "collected": 11
}

I defined a variables test_report and assigned the above value to it. But unable to use it in curl command? I'm seeing error as invalid_payload gcloud build logs

- name: gcr.io/cloud-builders/gcloud:latest
  entrypoint: 'bash'
  dir: 'workspace'
  env:
  - 'TEST_REPORT=${_TEST_REPORT}'
  args:
  - '-c'
  - |
    cd /workspace
#Installing jq
    apt-get update -y && apt-get install -y curl unzip groff jq bc less
#testing jq is successfully installed or not
    jq --help
    cat mezi_automation_test_report.json
#assgined summary object to test_report file
    jq .summary mezi_automation_test_report.json > test_report
#check test_report file is displayed with summary object or not
    cat test_report
    report=$(cat test_report)
#check report variable has summary object or not
    echo report

    curl --location --request POST '$_SLACK_WEBHOOK' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "channel":"$_CHANNEL",
        "username":"Cloudbuild",
        "icon_url":"$_ICON_URL",
        "text":"Test Report",
        "blocks": [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": " Test Report *`$_BRANCH`* branch on QA"
                }
            },
            {
                "type": "divider"
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                  "text": "Version: *$_VERSION*\n TEST_REPORT: *"${report}"*"
                }
            }
        ]
    }'

Upvotes: 0

Views: 308

Answers (1)

s4p3r_c1n0s
s4p3r_c1n0s

Reputation: 98

anything inside the ' single quote can not be interpreted or read as a regex, so you gotta close it first and then use any bash command/variable. After that you can start the single quote again to append the remaining part of the string/json.

NOTE: if the bash command has a special output like a json value, enclose it in a double quote "

test_report=$(jq .summary mezi_automation_test_report.json)
curl "http://localhost/send" -d '{"text":'"$test_report"' } '

or if you want more control, parse each data value separately

passed=$(jq .summary.passed mezi_automation_test_report.json)
total=$(jq .summary.total mezi_automation_test_report.json)
collected=$(jq .summary.collected mezi_automation_test_report.json)
curl "http://localhost/send" -d '{"text":"Test Report: \nPassed: '$passed' \nTotal: '$total' \nCollected: '$collected' \nEnd of Report" } '

Upvotes: 1

Related Questions