2

I'm writing a script to send a message to Slack using curl. Here's my code:

@echo off
@SETLOCAL EnableExtensions EnableDelayedExpansion
SET topic="Fw: *** Subject: Detail|detail|more-detail|detail:000"
echo %topic%
curl.exe -X POST -H "Content-Type: application/json" --data-urlencode "payload={\"channel\": \"#channelname\", \"username\": \"webhookbot\",\"text\": \"%topic\",\"icon_emoji\": \":ghost:\"}"  SLACK_WEBHOOK -k

Once I execute this script, I get the invalid_payload error along with my echo result. Can someone help me with this problem?

I have read this thread, but I'm still not quite sure how to solve it.

1
  • Is this Cygwin, or WSL, or something else? @echo off and curl.exe make this look like a Windows environment. Commented Jan 21, 2020 at 15:34

1 Answer 1

1

It seems you're using incomming webhook.

Choose one of following:

a) remove -H 'Content-type: application/json' or change it to -H 'Cotent-type: application/x-www-form-urlencoded'

curl.exe -X POST --data-urlencode "payload={\"channel\": \"#channelname\", \"username\": \"webhookbot\",\"text\": \"%topic\",\"icon_emoji\": \":ghost:\"}" SLACK_WEBHOOK_URL

b) keep content-type as it is, but change --data-urlencode to -d and remove payload= from the data

curl.exe -X POST -H "Content-Type: application/json" -d "{\"channel\": \"#channelname\", \"username\": \"webhookbot\",\"text\": \"%topic\",\"icon_emoji\": \":ghost:\"}" SLACK_WEBHOOK_URL

But if you use Slack API (for example /chat.postMessage):

1) you need to authorize with a token - add curl option -H "Authorization: Bearer YOUR_TOKEN_HERE"

2) don't use --data-urlencode, but -d

from docs: https://api.slack.com/methods/chat.postMessage

When POSTing with application/x-www-form-urlencoded data, the optional attachments argument should contain a JSON-encoded array of attachments. Make it easy on yourself and send your entire messages as application/json instead.

3) remove the from data payload= from data

curl.exe -X POST -H 'Content-Type: application/json' -H "Authorization: Bearer YOUR_TOKEN_HERE" -d "{ \"channel\": \"#channelname\", \"text\": \"message body\", \"username\": \"webhookbot\", \"icon_emoji\": \":ghost:\" }" "https://slack.com/api/chat.postMessage"
1
  • You can also use slack api with application/x-www-form-urlencoded. Same rules applies. Commented Jan 21, 2020 at 16:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.