REST服务上的错误请求错误使用POST(json数据)调用方法?

Posted

技术标签:

【中文标题】REST服务上的错误请求错误使用POST(json数据)调用方法?【英文标题】:Bad Request Error On REST service Method call with POST (json Data)? 【发布时间】:2013-12-17 23:55:39 【问题描述】:

您好,我是 RESTful WCF 的新手,我正在尝试使用 POST 对 Web 服务方法进行简单调用,这是我的代码

Service Interface code

 [ServiceContract]
public interface IJsonSave


    [OperationContract]
    [WebInvoke(UriTemplate = "/SaveJason", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
        Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string SaveJason(string value);

web.config 文件代码

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
   <behaviors>
     <serviceBehaviors>
       <behavior name ="servicebehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
   </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="restbehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<services>
  <service name ="JsonDataSave.JsonSave"
           behaviorConfiguration ="servicebehavior" >
    <endpoint name ="SOAPEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="basicHttpBinding"
              address ="soap" />
    <endpoint name ="RESTEndPoint"
              contract ="JsonDataSave.IJsonSave"
              binding ="webHttpBinding"
              address ="Rest"
              behaviorConfiguration ="restbehavior" />
    <endpoint contract="IMetadataExchange"
              binding="mexHttpBinding"
              address="mex" />
  </service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>

</configuration>

我尝试使用提琴手,她是我的标题

POST http://localhost:50267/JsonSave.svc/Rest/SaveJason
User-Agent: Fiddler
Content-Type: application/json
Data-Type: json
Host: localhost:50267

请求正文:

 ("value":"asdfasd")

它给出HTTP/1.1 400 Bad Request 的错误,并且当启用调试详细信息时,它会给出以下堆栈跟踪

The server encountered an error processing the request. The exception message is 'Error    in deserializing body of request message for operation 'SaveJason'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).'. See server logs for more details. The exception stack trace is: 
at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我在 Google 上搜索了很多,并尝试了所有可用的解决方案,但仍然没有任何帮助。它适用于 SOAP,只是在 REST 中出现错误!

【问题讨论】:

尝试改用BodyStyle = WebMessageBodyStyle.Bare @Atlasmaybe 尝试过仍会给出异常 @Atlasmaybe 如果不从 requestbody 发送任何内容,那么它会正确执行,但如果我发送参数 ("value":"sdfdas") 的值,则会引发异常 这可能是一个愚蠢的问题,但我们永远不知道...您是否在请求中发送数据长度? @Atlasmaybe 是的,我在标题中发送内容长度 【参考方案1】:

为了收集有关数据流和错误位置的其他信息,您可能会受益于在服务端启用 WCF 跟踪。以下链接应提供足够的详细信息:http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

关于您的场景中的错误,我怀疑客户端和服务的消息编码设置不一致。我想知道是否:

BodyStyle = WebMessageBodyStyle.WrappedRequest

应该是:

BodyStyle = WebMessageBodyStyle.Wrapped

BodyStyle = WebMessageBodyStyle.Bare

所以请求和响应具有相同的包装样式。

问候,

【讨论】:

嘿,谢谢BodyStyle = WebMessageBodyStyle.Wrapped 的帮助

以上是关于REST服务上的错误请求错误使用POST(json数据)调用方法?的主要内容,如果未能解决你的问题,请参考以下文章

POST 到 Jersey REST 服务收到错误 415 Unsupported Media Type

尝试在 https://api.thetvdb.com/ 向 TVDB REST API 发出 JSON POST 请求,但出现 CORS 错误

Angular2,REST 服务 POST 调用错误:“跨源请求被阻止”

Ajax POST:“服务器在处理请求时遇到错误”

AJAX POST 上的 JSON Stringify 400 错误请求

获取无效 POST、PATCH 的 CORS 错误 - REST 端点