Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending JSON to Slack in a HTTP POST request

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 result with JSON in a HTTP POST request.

I've been testing with both curl and Postman, but Slack doesn't seem to be acknowledging my request body at all.

{   "ok": false,   "error": "not_authed" } 

In curl, my request is encoded like this:

curl -H "Content-type: application/json" -X POST -d '{"token":"my-token-here","channel":"#channel-name-or-id","text":"Text here.","username":"otherusername"}' 

In Postman, this is the raw body:

{     "token":"my-token-here",     "channel":"#channel-name-or-id",     "text":"Text here.",     "username":"otherusername" } 

I haven't done anything like this before, so I'm not sure if I'm missing something out. Thanks!

like image 641
Valentine Avatar asked Aug 09 '15 14:08

Valentine


People also ask

How do I integrate API with Slack?

There are two ways for your app to integrate with Slack so that users can initiate and deal with Calls within Slack. One way for a user to initiate your app is via a Slash command—for example, by typing /mycallapp into the message composer. The other way for a user to initiate your app is directly via the Call icon.

How do you send a message to a Slack channel in bash?

Click on the “Webhooks incoming” button to turn it on. Create a new webhook for the application when sending messages. At the bottom of the page, click “Add New Webhook to Workspace.” Users will be prompted to approve the application and select the messaging channel.


2 Answers

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! :)

like image 63
finferflu Avatar answered Sep 28 '22 01:09

finferflu


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 
like image 27
LSerni Avatar answered Sep 28 '22 01:09

LSerni