Personalized WebApi Response

0

Hello, I have a WebApi service and would like to get an answer specifically with a Date, but by the time I call that service and I get the return from my PostAsync what I see is just an httpResult saying that was OK.

call to WebApi var result = await client.PostAsync (URI, content);

Return of the web method api return Request.CreateResponse (HttpStatusCode.OK, dataTest);

Whatever I send in return, I do not receive ...

I tried changing the method's return type to DateTime, or string, but it did not work either. Currently it is an HttpResponseMessage m

    
asked by anonymous 01.06.2017 / 00:47

1 answer

0

See if this is something you need:

 [HttpPost]
 public IHttpActionResult Post([FromBody]DateTime newDate)
 {
      return Ok<DateTime>(newDate);
 }

The OK<T>() method is within the ApiController class used as the base of your WebApi and is already responsible for serializing the parameter.

To read the content in C # you should go like this:

using (var http = new HttpClient())
{               
    var requestBody = new HttpRequestMessage
    {
        Method = HttpMethod.Post,
        RequestUri = url,
        Content = new StringContent(body)
    };
    requestBody.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json") { CharSet = "UTF-8" };

    var httpResult = http.SendAsync(requestBody).GetAwaiter().GetResult();
    var resultContent = httpResult.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    var resultStatus = (httpResult.StatusCode == HttpStatusCode.OK);
    DateTime? myDate;
    if (resultStatus)
        myDate= JsonConvert.DeserializeObject<DateTime>(resultContent, JSettings);
    else
        myDate= null;
}

Abs.

    
01.06.2017 / 02:40