Go Down

Topic: How to POST a json message to Slack WebHook (Read 220 times) previous topic - next topic

e_marcio

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

Code: [Select]

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. 

Code: [Select]

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.

http://webhook.site

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

PaulS

Code: [Select]
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.
The art of getting good answers lies in asking good questions.

e_marcio

Hi @PaulS

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

thanks

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

e_marcio

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


}

Go Up