Newtonsoft.Json.JsonReaderException When doing a POST

1

I'm having trouble doing the post in my API my app until it registers the object but then it crashes the application and closes with this execution error:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.

   public async Task<int> AddFrutas(Fruta fruta)
            {
                HttpClient client = new HttpClient();
                var data = JsonConvert.SerializeObject(fruta);
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://nivelamento.gopagoda.io/api/frutas", content);

                var result = JsonConvert.DeserializeObject<int>(response.Content.ReadAsStringAsync().Result);
                return result;

            }
    
asked by anonymous 05.06.2017 / 00:23

1 answer

0

The problem was when it was time to deserialize the object, I changed it to dynamic, and solved my problem.

public async Task<dynamic> AddFrutas(Fruta fruta)
{
    HttpClient client = new HttpClient();
    var data = JsonConvert.SerializeObject(fruta);
    var content = new StringContent(data, Encoding.UTF8, "application/json");

    var response = await client.PostAsync("http://nivelamento.gopagoda.io/api/frutas", content);

    var result = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
    return result;

}
    
05.06.2017 / 01:00