7

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, payload@filename.txt then it works, but I really need for the json to be dynamic.

Can anyone suggest what is wrong here?

chrismay
  • 1,384
  • 2
  • 15
  • 25
  • Thanks for asking this question. I have been beating my head against Slack's cURL examples not working until I realized it might be because it's Windows and searched Google with the right terms to find this post. – cyclingLinguist Nov 13 '21 at 19:32

3 Answers3

11

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
JieTing Xu
  • 111
  • 1
  • 2
6

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.

chrismay
  • 1,384
  • 2
  • 15
  • 25
0

Mine is working fine with the quoting.

curl -X POST --data-urlencode 'payload={"channel": "#general", "username": "webhook", "text": "Some message.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/BLAHBLAH/BLAHBLAHBLAH/BLAHBLAHBLAHBLAH

However, when trying to use it with cURL 7.15 doesn't work anymore, maybe because the --data-urlencode param is not supported in that version, and it doesn't seem to work without it.

Currently using it with cURL 7.29, and it's working fine.

Yahir Cano
  • 83
  • 2
  • 5