1

I am trying to insert a variable into my json payload (working in shell script) but I am unsure of how to escape the characters properly

I've tried many different escape methods but I'm a total noob at it and I either return the literal string or it doesn't run

SLACK_ALERT_WEBHOOK=desiredurl

curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
        {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK"
                    }
                }
            ]
        }
    ]}' "$SLACK_ALERT_WEBHOOK"

I just want to insert the value of SLACK_ALERT_WEBHOOK into this portion of the code "text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK but it either doesn't run or returns the literal string. I have the bottom "$SLACK_ALERT_WEBHOOK" working successfully at the bottom to send to my desired slack channel so I'm not worried about that.

I've got it working thanks to tripleee:

curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"*Verification*\", \"attachments\": [{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"mrkdwn\",\"text\": \"$SLACK_ALERT_WEBHOOK\"}}]}]}" $SLACK_ALERT_WEBHOOK
  • Do you really expect the `curl` command to return a shell command line you are sure is safe to execute as a shell command on your computer? If not, the `$(...)` around the `curl` is horribly wrong. – tripleee Mar 23 '19 at 07:46
  • I apologize, I had copy pasted a part of my code and missed that. I had been passing the result of that command into a variable to verify whether the webhook was successful. I've edited my post to make more sense. I'm looking into the duplicate post but mine is slightly more complicated (my own fault for setting it up that way) – Brandon Konieczny Mar 23 '19 at 12:47
  • The answer is simple in any event. Perhaps see also [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Mar 23 '19 at 13:04
  • Okay, from what I'm getting from that link is that the answer lies in that I have single quoted the entire payload – Brandon Konieczny Mar 23 '19 at 13:23
  • Okay I am trying the following now by removing the single quotes. #!/bin/bash SLACK_ALERT_WEBHOOK=desiredurl curl -X POST -H 'Content-type: application/json' --data "{"text": "*Verification*", "attachments": [{"blocks": [{"type": "section","text": {"type": "mrkdwn","text": "$SLACK_ALERT_WEBHOOK"}}]}]}" $SLACK_ALERT_WEBHOOK but it is returning invalid_payload – Brandon Konieczny Mar 23 '19 at 13:36
  • The second double quote in`"{"` terminates the quoted string. If you want literal double quotes in the JSON, you have to escape them; or paste adjacent single- and double-quoted string like `'"literal doubde quotes": "'"double-quoted $variable"'", 'more lnteral double quotes"'`. See also the "seesaw" paragraph in my answer to the question I linked in the comment above. – tripleee Mar 23 '19 at 13:58
  • Thank you so much!!!!! I finally have it working as expected. I appreciate your patience. I used: curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"*Verification*\", \"attachments\": [{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"mrkdwn\",\"text\": \"$SLACK_ALERT_WEBHOOK\"}}]}]}" $SLACK_ALERT_WEBHOOK – Brandon Konieczny Mar 23 '19 at 14:08

1 Answers1

-1

Try this:

SLACK_ALERT_WEBHOOK=desiredurl

$(curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
        {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Slack post failed for webhook, please investigate: `echo $SLACK_ALERT_WEBHOOK`"
                    }
                }
            ]
        }
    ]}' "`echo $SLACK_ALERT_WEBHOOK`")

Using the echo <<variable>> it should just run that as a command. I tested it on my own server, but might work differently on yours. Hope it helps.

  • Thank you for the feedback! I've tried implementing your `echo $SLACK_ALERT_WEBHOOK`but I am still running into the same issue with the channel displaying echo $SLACK_ALERT_WEBHOOK rather than desiredurl (although the '' has added some text formatting if you are familiar with slack). I'm thinking I may need to change my approach and separate out the variable from that text line. – Brandon Konieczny Mar 23 '19 at 02:26
  • The backticks do nothing useful inside single quotes. – tripleee Mar 23 '19 at 07:47
  • It might be worth looking at using Python instead? – Richard Annand Mar 23 '19 at 13:48
  • agreed but im adding additional code into an existing app so that ship has sailed – Brandon Konieczny Mar 23 '19 at 14:10