COSM触发器使用HTTP Post中的C#提取JSON值

时间:2013-04-12 23:16:57

标签: c# json triggers cosm

尝试通过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是我的班级镜像COSM Trigger fields

我的COSM Feed是here

我从COSM门户开始触发触发器,基于Twitter的触发器工作正常,HTTP Post到我的URL不是。如何调试?

2 个答案:

答案 0 :(得分:1)

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#的人会出现什么问题。

答案 1 :(得分:0)

经过测试和运作的代码 -

[OperationContract]  
[WebInvoke(Method = "POST",  
BodyStyle = WebMessageBodyStyle.WrappedRequest,  
UriTemplate = "cosm")]  
string cosmStream(Stream body);