我使用以下post方法开发了WCF服务:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/InsertBearingData")]
bool InsertBearingData(String JSONString);
我正在使用Fiddler为此方法制定HTTP POST请求,但它正在返回Status Code - 400 Bad Request
。这是提出的要求:
请求标题:
Host: localhost:21468
Content-Length: 96
Content-Type: application/json
请求正文:
[{"start time":"29-03-2013 11:20:11.340","direction":"SW","end time":"29-03-2013 11:20:14.770"}]
您能告诉我如何制定一个好的请求以获得成功的回应吗?
答案 0 :(得分:3)
您的代码中存在一些问题:
Wrapped
,这意味着参数应该包装在一个对象中,其键是参数名称,类似于{"JSONString":<the actual parameter value>}
要接收与您要发送的请求类似的请求,您需要执行如下操作:
[ServiceContract]
public interface ITest
{
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/InsertBearingData")]
bool InsertBearingData(MyType[] param);
}
[DataContract]
public class MyType
{
[DataMember(Name = "start time")]
public string StartTime { get; set; }
[DataMember(Name = "end time")]
public string EndTime { get; set; }
[DataMember(Name = "direction")]
public string Direction { get; set; }
}