Arduino发送POST给Slack WebHook

时间:2018-06-10 02:37:16

标签: http arduino http-headers

我有一个使用WebHook的Slack频道设置,基本上我可以运行POST并向我的频道发送消息。在我的桌面上使用HTTP工具时,一切正常,但我无法从Arduino UNO + Ethernet Shield发送POST。

这是我的代码。是的,我正在分享我的webHookURL,不用担心,这只是一个测试: - )

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("");

我得到了一个HTTP 400:

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支持小组回答我这可能是一个不正确的POST,但我无法看到。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

似乎JSON对象格式不正确...你可以共享传递给slack webhooks的对象,这将有助于进一步理解..只是看上面的代码postData变量不是JSON格式 - 这会导致HTTP 400 Bad Request,你可以使用#include等其他库来检查...

使用curl请求检查松弛webhook会给出正确的输出..您可以在松弛通道上检查

cURL请求:         curl -X POST -H&#39;内容类型:application / json&#39; \         --data&#39; {&#34; text&#34;:&#34;这是一行文字。\ n这是另一个。&#34;}&#39; \             https://hooks.slack.com/services/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

    https://hooks.slack.com/services/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
    10 June 2018, 03:04 UTC
    Results from running: curl --data {"text":"This is a line of text.\nAnd this is another one."} --header Content-type: application/json --request POST https://hooks.slack.com/services/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 
    Response Header
    HTTP/1.1 200 OK
    Content-Type: text/html
    Transfer-Encoding: chunked
    Connection: keep-alive
    Date: Sun, 10 Jun 2018 03:04:33 GMT
    Server: Apache
    Vary: Accept-Encoding
    X-Slack-Backend: h
    Referrer-Policy: no-referrer
    Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
    X-Frame-Options: SAMEORIGIN
    Access-Control-Allow-Origin: *
    X-Via: haproxy-www-19gm
    X-Cache: Miss from cloudfront
    Via: 1.1 a9ced60f02a91a154a8631077a254a91.cloudfront.net (CloudFront)
    X-Amz-Cf-Id: XAQZ5ZSfnLCz_wOxmNbma2Q0q0GUZM_uC052kTuVglGJmGvo8AxIpg==

    Response Body
    ok