Val*_*ine 40 api json slack-api
我正在尝试使用Slack的chat.postMessage API调用发送消息.我在HTTP GET中编码测试消息没有问题,但我试图在HTTP POST请求中使用JSON实现相同的结果.
我一直在curl和Postman一起测试,但Slack似乎根本没有承认我的请求体.
{
"ok": false,
"error": "not_authed"
}
Run Code Online (Sandbox Code Playgroud)
在curl,我的请求编码如下:
curl -H "Content-type: application/json" -X POST -d '{"token":"my-token-here","channel":"#channel-name-or-id","text":"Text here.","username":"otherusername"}'
Run Code Online (Sandbox Code Playgroud)
在邮递员中,这是原始身体:
{
"token":"my-token-here",
"channel":"#channel-name-or-id",
"text":"Text here.",
"username":"otherusername"
}
Run Code Online (Sandbox Code Playgroud)
我之前没有做过这样的事情,所以我不确定我是否遗漏了一些东西.谢谢!
fin*_*flu 64
我有点晚了,但我希望这可以帮助其他像我一样偶然发现这个问题的人.我刚刚与Slack保持联系,这就是他们告诉我的:
Slack Web API根本不接受JSON数据 - 因此,在更改Content-Type时,应使用标准HTTP表单属性发布这些变量.
我们计划在未来支持JSON数据,以确保未来的一致性.
因此,您的cURL行应如下所示:
curl -X POST -d 'token=my-token-here&channel=#channel-name-or-id&text=Text here.&username=otherusername'`
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!:)
好的,重新阅读文档后我发现它:
JSON编码的主体
对于这些写入方法,您也可以将HTTP POST数据作为Content-type:application/json发送.
有一些基本规则:
- 您必须将Content-type HTTP标头显式设置为application/json.没有它,我们不会解释你的POST主体.
- 您必须在Authorization HTTP标头中将令牌作为承载令牌进行传输.
- 您不能将令牌作为查询字符串的一部分发送,也不能作为发布的JSON中的属性发送.
- 不要在查询字符串,URL编码的POST正文和JSON属性之间混合参数.每个请求选择一种方法.
- 为属性提供显式空值将导致为其分配默认行为.
而这个curl例子.请注意Authorization标头.
curl -X POST \
-H 'Authorization: Bearer xoxb-1234-56789abcdefghijklmnop' \
-H 'Content-type: application/json; charset=utf-8' \
--data '{"channel":"C061EG9SL","text":""..."}' \
https://slack.com/api/chat.postMessage
Run Code Online (Sandbox Code Playgroud)
这可能不符合完整答案的条件,但如果目的是发送消息附件,则可以发送urlencoded JSON结构作为attachments参数的值,如此(为清晰起见,分成多行):
https://slack.com/api/chat.postMessage?
token=YOUR-TOKE-N000&
channel=%23alerts&
text=Hi&
attachments=%5B%7B%22color%22%3A%22good%22%2C%22fallback%22%3A%22plain+text%22%2C%22text%22%3A%22colored+text%22%7D%5D
Run Code Online (Sandbox Code Playgroud)
值的值attachments是URL编码的[{"color":"good","fallback":"plain text","text":"colored text"}].您应该能够使用此处描述的所有附件属性.
Slack 已更新,现在可以使用了。试试这个例子:
curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://hooks.slack.com/services/AAAAAA/BBBBBB/CCCCCC
Run Code Online (Sandbox Code Playgroud)
有关文档,请参阅https://api.slack.com/incoming-webhooks。
As of March 2018, Slack now supports POST with JSON body
终点:https://slack.com/api/chat.postMessage
标头:Authorization: Bearer xoxp-your-hardly-find-token-here
主体:{"channel":"CH9NN37","text":"something from me!"}
注意 承载 在 授权 报头中。
| 归档时间: |
|
| 查看次数: |
42828 次 |
| 最近记录: |