COSM触发器使用c#从HTTP Post中提取JSON值
本文关键字:Post 提取 JSON HTTP 触发器 COSM | 更新日期: 2023-09-27 18:08:50
尝试通过c# RESTful服务接收COSM触发器HTTP Post,未收到警报。我采用了COSM API JSON有效负载,并在测试客户端中使用了它——这是有效的。当我设置提要并尝试调试触发器测试,或者只是强制触发器正常触发时,我的REST服务不会被调用。如果我尝试任何形式的测试客户端,服务都可以很好地处理JSON POST。
c#服务在这里:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "cosm",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string CosmAlert(CosmTrigger data);
其中CosmTrigger是我的类,镜像了cosmm Trigger字段。
我的COSM提要在这里。
我从COSM门户开始触发一个触发器,基于Twitter的触发器工作得很好,HTTP Post到我的URL不是。如何调试这个?
Cosm触发器目前不以JSON主体的形式发送触发器,它实际上发送一个URL编码的请求,其中JSON触发器主体编码为一个名为'body'的参数。
在测试触发器上使用http://requestb.in,并查看原始输出,基本请求如下所示:
POST /103s0dh1 HTTP/1.1
X-Request-Id: e05e9d699edbd5f584fc491cf9416df747be4df4
User-Agent: Cosm Deliverator (Axino/0.3.02) - https://cosm.com
Host: requestb.in
Content-Type: application/x-www-form-urlencoded
Content-Length: 918
Connection: close
body=%7B%22id%22%3A7443%2C%22url%22%3A%22http%3A%2F%2Fapi.cosm.com%2Fv2%2Ftriggers%2F7443%22%2C%22type%22%3A%22lt%22%2C%22threshold_value%22%3A%2220%22%2C%22timestamp%22%3A%222013-04-14T09%3A50%3A47.590044Z%22%2C%22environment%22%3A%7B%22id%22%3A57346%2C%22feed%22%3A%22http%3A%2F%2Fapi.cosm.com%2Fv2%2Ffeeds%2F57346%22%2C%22title%22%3A%22Macbook+Battery+Level%22%2C%22description%22%3A%22%22%2C%22private%22%3Afalse%2C%22location%22%3A%7B%22lat%22%3Anull%2C%22lon%22%3Anull%2C%22name%22%3A%22%22%7D%7D%2C%22triggering_datastream%22%3A%7B%22id%22%3A%22battery%22%2C%22url%22%3A%22http%3A%2F%2Fapi.cosm.com%2Fv2%2Ffeeds%2F57346%2Fdatastreams%2Fbattery%22%2C%22at%22%3A%222013-04-14T09%3A50%3A02.406927Z%22%2C%22value%22%3A%7B%22max_value%22%3A1724.0%2C%22min_value%22%3A0.0%2C%22value%22%3A%2226.28%22%7D%2C%22units%22%3A%7B%22type%22%3Anull%2C%22symbol%22%3A%22%25%22%2C%22label%22%3Anull%7D%7D%2C%22debug%22%3Atrue%7D
不幸的是,我不是c#专家,但我怀疑问题是与服务没有配置为从标准的urlencoded请求体中提取JSON体有关,但也许这可能会为了解c#的人提供线索,以找出问题所在。
测试和工作代码-
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "cosm")]
string cosmStream(Stream body);