Search code examples
slack-apislack

Posting attachments to Slack API


So I just understood that the Slack Web API does not support JSON data over POST. Which means I have to encode my complex and nested JSON object to fit in query parameters over GET. Problem is, the attachements don't seem to work. Does anyone have a solution ?


Solution

  • So I just understood that the Slack Web API does not support JSON data over POST. Which means I have to encode my complex and nested JSON object to fit in query parameters over GET.

    I'm not sure I follow what you mean. You can certainly use POST. The body of a Slack API call should be form-encoded, but parameter values are sometimes JSON (as is the case for attachments).

    Here's a working curl command that uses HTTP POST to post a message with a simple attachment.

    $ curl -d token=<REDACTED> -d channel=<REDACTED> \
      -d text="This is the main text." \
      -d attachments='[{"text": "This is an attachment."}]' \
      https://slack.com/api/chat.postMessage
    

    I'd recommend using POST, but GET also works fine. If you fill in the values in https://api.slack.com/methods/chat.postMessage/test, the tool will give you a URL at the bottom that you can use with HTTP GET.