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

查看:373
本文介绍了在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.

我一直在使用curl邮递员进行测试,但是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"}'

在邮递员中,这是原始内容:

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天全站免登陆