在 HTTP POST 请求中将 JSON 发送到 Slack [英] Sending JSON to Slack in a HTTP POST request

查看:38
本文介绍了在 HTTP POST 请求中将 JSON 发送到 Slack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Slack 的 chat.postMessage API 调用发送消息.我在 HTTP GET 中编码我的测试消息没有问题,但我试图在 HTTP POST 请求中使用 JSON 实现相同的结果.

I'm trying to send a message using Slack's chat.postMessage API call. I have no problems encoding my test messages within HTTP GET, but I'm trying to achieve the same result with JSON in a HTTP POST request.

我一直在用 curlPostman 进行测试,但 Slack 似乎不是完全确认我的请求正文.

I've been testing with both curl and Postman, but Slack doesn't seem to be acknowledging my request body at all.

{
  "ok": false,
  "error": "not_authed"
}

curl中,我的请求是这样编码的:

In curl, my request is encoded like this:

curl -H "Content-type: application/json" -X POST -d '{"token":"my-token-here","channel":"#channel-name-or-id","text":"Text here.","username":"otherusername"}'

在 Postman 中,这是原始正文:

In Postman, this is the raw body:

{
    "token":"my-token-here",
    "channel":"#channel-name-or-id",
    "text":"Text here.",
    "username":"otherusername"
}

我以前没有做过这样的事情,所以我不确定我是否遗漏了什么.谢谢!

I haven't done anything like this before, so I'm not sure if I'm missing something out. Thanks!

推荐答案

我来的有点晚,但我希望这可以帮助其他像我一样偶然发现这个问题的人.我刚刚与 Slack 取得了联系,他们是这样告诉我的:

I'm a bit late, but I hope this can help other people who stumble into this issue like me. I've just been in touch with Slack, and this is what they told me:

Slack Web API 根本不接受 JSON 数据 — 因此,除了更改 Content-Type 之外,还应使用标准 HTTP 表单属性发布这些变量.

The Slack Web API doesn't accept JSON data at all — so along with changing the Content-Type those variables should be posted using standard HTTP form attributes.

我们计划在未来支持 JSON 数据以保证未来的一致性.

We plan to support JSON data in the future for consistency in the future.

因此,您的 cURL 行应如下所示:

So, your cURL line should look like:

curl -X POST -d 'token=my-token-here&channel=#channel-name-or-id&text=Text here.&username=otherusername'`

我希望这会有所帮助!:)

I hope this helps! :)

这篇关于在 HTTP POST 请求中将 JSON 发送到 Slack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆