How to POST a json message to Slack WebHook

Hi all,

I have a Slack channel setup with a WebHook, basically I can run a POST and send a message to my channel. All works fine when using an HTTP tool in my desktop but I am unable to send a POST from Arduino UNO + Ethernet Shield.

This is my code. Yes, I am sharing my webHookURL, no worries, this is only a test :slight_smile:

String PostData = "{\"text\": \"Agora Vai\"}";

Serial.println(PostData);

client.println("POST /services/TAXFFU91T/BAXFMF8BF/LPH1b60RsQpOGvur2ZNn0zpz HTTP/1.1");
client.println("Host: hooks.slack.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.print(PostData.length());
client.println("\n");
client.println(PostData);
client.println("");

I´ve tried lots of ways and samples but I keep getting a HTTP 400 error.

HTTP/1.1 400 Bad Request
Server: CloudFront
Date: Sun, 10 Jun 2018 02:30:37 GMT
Content-Type: text/html
Content-Length: 556
Connection: close
X-Cache: Error from cloudfront
Via: 1.1 b7a41f8635d9e8e6499fbcc9612f2ec5.cloudfront.net (CloudFront)
X-Amz-Cf-Id: z8WMFOiFRe1nZnSzNbFdSIGriOhh0vv1TxbxFrkuZ9JUoPnssnBbrQ==

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>400 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.

Slack support says my post is mal-formed so Slack can´t recognize it and returns 400.

I can't see what's wrong but I know it works fine from my desktop when posting the exactly same payload from NetTool or SoapUI.

My last attempt was trying a different server to assess my post, but I the website below cannot even see the incoming POST request.

This is a pretty cool website where we can configure a webhook for testing, send a POST and watch the incoming message - Unfortunately I can't see nothing coming from my Arduino UNO.

Any idea on what I may be missing ?

Thanks in Advance.

Márcio

client.print("Content-Length: ");
client.print(PostData.length());
client.println("\n");
client.println(PostData);
client.println("");

The order of data is VERY important. The extra \n is screwing things up.

You should println() the data, then println() with NOTHING in the parentheses, then println() the post data, then println() with NOTHING in the parentheses.

Hi @PaulS

Thanks for your reply... I just didnt get why should I println postdata twice. Was that just a typo?

thanks

I just didnt get why should I println postdata twice. Was that just a typo?

I meant that you should println() the length of the data, println() a blank line, then println() the data.

PaulS:
I meant that you should println() the length of the data, println() a blank line, then println() the data.

Ok, got it now... unfortunately still not going thru...

This is my whole code

if (client.connect(host, 443))
{

Serial.println("Connected to Slack");

char PostData[] = "{"text": "Nivel de Agua em 50"}"; // your JSON payload

Serial.println(PostData);

Serial.println("Connected to Slack");
client.println("POST /services/TAXFFU91T/BAXFMF8BF/LPH1b60RsQpOGvur2ZNn0zpz HTTP/1.1");
client.println("Host: hooks.slack.com"); //
client.println("User-Agent: Arduino/unowifi");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(strlen(PostData));// number of bytes in the payload
client.println();// important need an empty line here
client.println(PostData);// the payload
client.println();// important need an empty line here

}

Did you ever find a solution to this? I'm trying the same thing.

knussear:
Did you ever find a solution to this? I'm trying the same thing.

I got this to work today on my ESP32.
I'm building a sensor to determine if there's something in my outside mailbox, then alert me on Slack.

include <SPI.h>
#include <WiFi.h>
#include <SSLClient.h>
#include "trust_anchors.h" // READ THIS https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md
.
.
.
void postSlackMessage() {
  char server[] = "hooks.slack.com";
  // specify the server and port, 443 is the standard port for HTTPS
  if (client.connect(server, 443)) {
    // Make a HTTP request:
    char postData[] = "{\"icon_emoji\": \":mailbox:\", \"text\": \"There's mail in the box!\"}";
    client.println("POST /services/XXX/YYY/ZZZ HTTP/1.1");
    client.println("Host: hooks.slack.com");
    client.println("User-Agent: ESP32/SSLClient");
    client.println("Content-Type: application/json");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(strlen(postData));// number of bytes in the payload
    client.println();                // important need an empty line here
    client.println(postData);        // the payload in JSON format. See https://api.slack.com/messaging/webhooks#advanced_message_formatting for additional options
    client.println();                // important need an empty line here
    client.print("Host: ");
    client.println();
    beginMicros = micros();}
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

The response from Slack was fine too:

HTTP/1.1 200 OK
date: Thu, 10 Sep 2020 19:40:08 GMT
server: Apache
vary: Accept-Encoding
strict-transport-security: max-age=31536000; includeSubDomains; preload
referrer-policy: no-referrer
x-slack-backend: r
x-frame-options: SAMEORIGIN
access-control-allow-origin: *
connection: close
transfer-encoding: chunked
content-type: text/html
x-via: haproxy-www-cyj4,haproxy-edge-lhr-ltnf