I'm trying to post a message to a Slack channel. Slack provides an example of a cURL command, but running this verbatim doesn't work.
The command provided is:
curl -X POST --data-urlencode 'payload={"channel": "#deployment", "username": "webhookbot", "text": "This is posted to #deployment and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/SomeCode/OtherCode/3rdCode
I've installed the latest cURL on my machine (running windows 8.1), and when running the script above I get:
curl: (6) Could not resolve host: #deployment,
curl: (6) Could not resolve host: username
curl: (6) Could not resolve host: webhookbot,
etc.
I've thought it might be something about how windows console is dealing with the single and double quotes, but I've been unable to get it to work.
I've found that if I replace the json string with a file, [email protected] then it works, but I really need for the json to be dynamic.
Can anyone suggest what is wrong here?
All quoting handled by cmd.exe is done with double quotes except to enclose the command to run within a FOR /F statement. So your cURL command running on Windows shall to be like:
curl -X POST --data-urlencode "payload={'channel': '#deployment', 'username': 'webhookbot', 'text': 'This is posted to #deployment', 'icon_emoji': ':ghost:'}" https://hooks.slack.com/services/Code1/Code2/Code3
Figured it out.
I needed to change the single quotes wrapping the entire JSON string to double quotes, and then the way to escape the single quotes in this context is not \" or ^" but "".
So this worked:
curl -X POST --data-urlencode "payload={""channel"": ""#deployment"", ""username"": ""webhookbot"", ""text"": ""This is posted to #deployment"", ""icon_emoji"": "":ghost:""}" https://hooks.slack.com/services/Code1/Code2/Code3
I hope this saves someone else the time I spent with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With