Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack provided cURL command not working to post inline json

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?

like image 272
chrismay Avatar asked Nov 16 '15 04:11

chrismay


2 Answers

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
like image 145
JieTing Xu Avatar answered Sep 21 '22 08:09

JieTing Xu


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.

like image 24
chrismay Avatar answered Sep 23 '22 08:09

chrismay