I\'m trying to send a message using Slack\'s chat.postMessage API call. I have no problems encoding my test messages within HTTP GET, but I\'m trying to achieve the same res
I did this in powershell and it works like a charm.
$url="https://slack.com/api/chat.postMessage"
$messageContent= # your message here
$token = # your token here
$channel = # channel name
$opt_username= # optional user name
$body = @{token=$token;channel=$channel;username=$opt_username;text=$messageContent;pretty=1}
try
{
Invoke-WebRequest -Uri $url -Method POST -Body $body
}
catch
{
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
I'm a bit late, but I hope this can help other people who stumble into this issue like me. I've just been in touch with Slack, and this is what they told me:
The Slack Web API doesn't accept JSON data at all — so along with changing the Content-Type those variables should be posted using standard HTTP form attributes.
We plan to support JSON data in the future for consistency in the future.
So, your cURL line should look like:
curl -X POST -d 'token=my-token-here&channel=#channel-name-or-id&text=Text here.&username=otherusername'`
I hope this helps! :)
As of March 2018, Slack now supports POST with JSON body
Endpoint: https://slack.com/api/chat.postMessage
Headers: Authorization: Bearer xoxp-your-hardly-find-token-here
Body: {"channel":"CH9NN37","text":"something from me!"}
Notice the Bearer in the Authorization header.
Okay, after re-reading the documentation I found it:
JSON-encoded bodies
For these write methods, you may alternatively send your HTTP POST data as Content-type: application/json.
There are some ground rules:
- You must explicitly set the Content-type HTTP header to application/json. We won't interpret your POST body as such without it.
- You must transmit your token as a bearer token in the Authorization HTTP header.
- You cannot send your token as part of the query string or as an attribute in your posted JSON.
- Do not mix arguments between query string, URL-encoded POST body, and JSON attributes. Choose one approach per request.
- Providing an explicitly null value for an attribute will result in whichever default behavior is assigned to it.
And the curl
example. Notice the Authorization header.
curl -X POST \
-H 'Authorization: Bearer xoxb-1234-56789abcdefghijklmnop' \
-H 'Content-type: application/json; charset=utf-8' \
--data '{"channel":"C061EG9SL","text":""..."}' \
https://slack.com/api/chat.postMessage
Try putting each property in its own -d parameter, like so:
curl https://slack.com/api/chat.postMessage -X POST -d "channel=#tehchannel" -d "text=teh text" -d "username=teh user" -d "token=teh-token-you-got-from-teh-page-that-machinehead115-linked-to" -d "icon_emoji=:simple_smile:"
Below curl command worked for me.
curl -X POST -H 'Authorization: Bearer xoxb-41234078565-14457098702432-ZLJj9UFOZKnOJtjNW4dv3ukG' -H 'Content-type: application/json; charset=utf-8' --data '{"channel":"#general","text":"I hope the tour went well, Mr. Wonka."}' https://slack.com/api/chat.postMessage