【问题标题】:Arduino Send POST to Slack WebHookArduino 向 Slack WebHook 发送 POST
【发布时间】:2018-06-10 02:37:15
【问题描述】:

我有一个带有 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,但我看不出在哪里。

有什么想法吗?

【问题讨论】:

    标签: http arduino http-headers


    【解决方案1】:

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

    使用 curl 请求检查 slack webhook 会得到正确的输出。你可以在 slack 频道上检查

    cURL 请求: curl -X POST -H '内容类型:应用程序/json' \ --data '{"text":"这是一行文本。\n这是另一行。"}' \ 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
    

    【讨论】:

    • 您好 Sanjay,感谢您的意见...是的,我看到了您发送到我的频道的两个测试。我也有同样的问题,我唯一的问题是在 Arduino 代码中。据我了解,通过发送该格式的 postData 字符串将代表一个 Json 对象/字符串,但显然这不会发生......有趣的是,我也从网络上的其他示例中得到了这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2019-01-23
    相关资源
    最近更新 更多