简体   繁体   English

处理来自Slack的发布请求

[英]Processing Post Request from Slack

I've setup an asp.net MVC web application to capture post request. 我已经设置了一个asp.net MVC Web应用程序来捕获发布请求。 I've set breakpoints to see what info is coming through. 我已设置断点以查看正在传递的信息。 When an interactive button is clicked, my breakpoint is hit but the object has no details. 单击交互式按钮时,会触发我的断点,但该对象没有详细信息。 It simply says {Object} when i hover over value . 当我将鼠标悬停在value时,它只是说{Object}。 When I use Postman and send a raw JSON body, it displays the JSON. 当我使用Postman并发送原始JSON主体时,它会显示JSON。 May I ask how am i suppose to process the slack object that is sent? 请问我怎么处理发送的松弛对象?

public void Post(object value)
{
    // breakpoint here
}

For interactive messages Slack send a standard POST request with a single form-data encoded parameter called payload in the body. 对于交互式消息,Slack发送标准POST请求,其中包含一个名为payload的单一表单数据编码参数。 This parameter contains the data of the actual request as JSON string. 此参数包含实际请求的数据作为JSON字符串。

You can simulate this with Postman by using the following parameters: 您可以使用以下参数使用Postman模拟这个:

  • Request type: POST 请求类型:POST
  • Header: Content-Type: application/x-www-form-urlencoded 标题: Content-Type: application/x-www-form-urlencoded
  • Body: form-data , with key = payload and value = JSON string. 正文: form-datakey = payloadvalue = JSON字符串。

So the body is not fully JSON as you assumed, but contains a form parameter with a JSON encoded string. 因此,正如您所假设的那样,正文不是完全JSON,而是包含带有JSON编码字符串的表单参数。 If you change your app accordingly, it will work. 如果您相应地更改了应用程序,它将起作用。

In c# you can access the content of the payload parameter with Request["payload"] (see this answer or details). 在c#中,您可以使用Request["payload"]访问payload参数的内容(请参阅此答案或详细信息)。

Then you still need to decode the JSON string into an object of course. 然后,您仍然需要将JSON字符串解码为对象。 An easy approach is to use JavaScriptSerializer.Deserialize . 一种简单的方法是使用JavaScriptSerializer.Deserialize (see this answer for details and alternative approaches). (有关详细信息和替代方法,请参阅此答案 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM