Invalid request from WCF service using httpclient postAsJsonAsync

Below is my wcf service.

  public ApiResponseWrapper<TextBlobModel> PostText(string sessionId, string profileId, TextBlobModel txtModel)
    {}

The interface part for this

 [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "session/{sessionId}/profile/{profileId}/text")]
    ApiResponseWrapper<TextBlobModel> PostText(string sessionId, string profileId, TextBlobModel txtModel);

And model

 [DataContract]
public class TextBlobModel
{
    [DataMember]
    public string text { get; set; }

   [DataMember]
    public DateTime receivedTime { get; set; }
}

When I called the service above as follows, I always get a bad request error.

          var baseApiUrl = "http://localhost:51398/api.svc/";
           HttpClient authClient = new HttpClient();
           authClient.BaseAddress = new Uri(baseoAuthApiUrl);
           apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

           var txtModel = new TextBlobModel();
           txtModel.text = "Hello How are you";
           txtModel.receivedTime = DateTime.Now;

            HttpResponseMessage txtResponse = apiClient.PostAsJsonAsync(String.Format("session/{0}/profile/{1}/text", "sessionId", "profileId"), txtModel).Result;
            var txtData = txtResponse.Content.ReadAsAsync<RootObject>().Result;

See the following image.

enter image description here

Could you suggest what I am doing wrong here?

+4
source share
1 answer

Try this, change the UriTemplate

"session/{sessionId}/profile/{profileId}/text"

to

"PostText/session/{sessionId}/profile/{profileId}/text".

try this "BodyStyle = WebMessageBodyStyle.WrappedRequest",

and add this to the configuration file to add the trace log, after which you will get the actual error.

<configuration> 
<system.diagnostics>
<trace autoflush="true">
</trace> 
<sources> 
<source name="System.ServiceModel" switchValue="Critical,Information,ActivityTracing" propagateActivity="true">
<listeners> 
<add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\logs\messages.svclog" />
</listeners>
</source> 
</sources> 
</system.diagnostics>
</configuration>
+1
source

All Articles