Sending JSON to Slack in a HTTP POST request

Valentine picture Valentine · Aug 9, 2015 · Viewed 50.2k times · Source

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!

Answer

finferflu picture finferflu · Oct 23, 2015

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