Sending JSON to Slack in a HTTP POST request

55,019

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

This may not qualify for the complete answer, but if the purpose is to send a message attachment, you can send a urlencoded JSON structure as the value of the attachments parameter, like so (split into multiple lines for clarity):

https://slack.com/api/chat.postMessage?
token=YOUR-TOKE-N000&
channel=%23alerts&
text=Hi&
attachments=%5B%7B%22color%22%3A%22good%22%2C%22fallback%22%3A%22plain+text%22%2C%22text%22%3A%22colored+text%22%7D%5D

The value of attachments is URL-encoded [{"color":"good","fallback":"plain text","text":"colored text"}]. You should be able to use all attachment attributes described here.

Solution 5

Slack has been updated, this now works. Try this example:

curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://hooks.slack.com/services/AAAAAA/BBBBBB/CCCCCC

See https://api.slack.com/incoming-webhooks for documentation.

Share:
55,019

Related videos on Youtube

Valentine
Author by

Valentine

Learning to code, mostly in JavaScript and Go.

Updated on October 21, 2020

Comments

  • Valentine
    Valentine over 3 years

    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!

  • Robert Kaucher
    Robert Kaucher about 8 years
    I love how they actually never really spell this out in the documentation but then give every example in JSON as if a web developer is going to look at it and think, of course I'm going to form encode my JSON object and plug it on a query string. That just makes perfect sense.
  • xer0x
    xer0x almost 8 years
    This is so annoying. Some api calls even support sending curl -d 'payload={"json":"wtf"}'. The docs are way too ambiguous about this.
  • barbary
    barbary over 7 years
    This is just crazy all those Json examples and the api doesn't actualy take a Json object at all. They even have a message builder where you can build complex messages in Json but then in reality you have to carve the Json up into a querystring to make it really work.
  • pronoob
    pronoob about 7 years
    So it doesn't take JSON, but it responds with JSON.... that makes no sense!
  • StingyJack
    StingyJack about 7 years
    The documentation over at github is is also half json, half markdown
  • botbot
    botbot over 6 years
    this is so stupid and i'll never get back the 3 hours of my life i wasted on this
  • nickromano
    nickromano over 6 years
    Thank you, I was missing the Authorization header.
  • Scott
    Scott almost 6 years
    it is NOWHERE said, but absolutely required, to ALSO include the charset: -H 'Content-Type: application/json; charset=utf-8' \
  • LSerni
    LSerni almost 6 years
    I've been using this API since november 2017 - I wrote this answer to this very question on January 9: stackoverflow.com/a/48178295/1428679 - and as far as I can see, your example is exactly the same. Am I missing something, or was there some documentation kerfuffle?
  • We are Borg
    We are Borg about 5 years
    This helped me... :-)

Related