簡體   English   中英

REST服務上的錯誤請求錯誤使用POST(json數據)調用方法?

[英]Bad Request Error On REST service Method call with POST (json Data)?

嗨,我是RESTful WCF的新手,我正在嘗試使用POST對webservice方法進行簡單的調用,這是我的代碼

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錯誤!

為了收集有關數據流和錯誤位置的其他信息,您可以從服務端啟用WCF跟蹤中受益。 以下鏈接應提供足夠的詳細信息: http//msdn.microsoft.com/en-us/library/ms733025(v = vs.110).aspx

關於您的方案中的錯誤,我懷疑沒有為客戶端和服務一致地設置消息編碼。 我想知道是否可能:

BodyStyle = WebMessageBodyStyle.WrappedRequest

應該是:

BodyStyle = WebMessageBodyStyle.Wrapped

要么

BodyStyle = WebMessageBodyStyle.Bare

所以請求和響應具有相同的包裝樣式。

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM