0
I looked for other topics related to this error, but I believe my case is a little different.
Error:
System.Exception: Error processing request: Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Models.Request' because the type requires a JSON Object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON Object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that Implements a Collection interface (e.g. Icollection, Ilist) like List that can be deserialized from a JSON array. Jsonarrayattribute can also be Added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
Let’s go from the top:
I am trying to perform a POST and getting this error in the application. This error no longer makes sense, because in the post I do not perform any deserialization.
Testing on a Motorola Z3 Play.
Code that generates the error: ("client" is an instance of Httpclient() )
public async Task AddPedidoAsync(Pedido pedido)
{
try
{
string url = "http://<endereço>:55580/api/Pedidos";
string token = Preferences.Get("token", "default_value");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var data = JsonConvert.SerializeObject(pedido);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await client.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
throw new Exception("Erro ao incluir pedido");
}
}
catch (Exception ex)
{
throw new Exception("Erro na solicitação: " + ex.Message);
}
The class "requested":
public class Pedido
{
public int Id { get; set; }
public int IdEstabelecimento { get; set; }
public int IdCliente { get; set; }
public int IdStatus { get; set; }
public int IdFormaPagamento { get; set; }
public DateTime DataPedido { get; set; }
public decimal ValorTotal { get; set; }
public decimal ValorDesconto { get; set; }
public decimal ValorEntrega { get; set; }
public decimal ValorTroco { get; set; }
public bool Recepcionado { get; set; }
public DateTime? DataRecepcao { get; set; }
public int IdPedidoMobile { get; set; }
}
What I’ve already tested:
When debugging, initially I thought the error could be due to the structure of my JSON with regard to the "Requested Datafield", which is a Datetime:
{\"Id\":0,
\"IdEstabelecimento\":1,
\"IdCliente\":1,
\"IdStatus\":1,
\"IdFormaPagamento\":1,
\"DataPedido\":\"2019-01-29T10:40:52.539523-02:00\",
\"ValorTotal\":220.99,
\"ValorDesconto\":0.0,
\"ValorEntrega\":0.0,
\"ValorTroco\":0.0,
\"Recepcionado\":false,
\"DataRecepcao\":null,
\"IdPedidoMobile\":2975}
So I changed my Order class and converted the date to the format without the milliseconds. Nothing done.
To conclude in a very strange way:
If I query in my database, the request was posted correctly!
Which line blows the error? It seems to me an error in the web service and not in the application. Btw, when capturing an exception and launching only her message you throw away all relevant information concerning the error.
– Jéf Bueno
You don’t need to have a constructor without parameters in the Request class?
– vik
the line is : Answer = await client.Postasync(url, content);... And you gave me a great idea actually... I’m going to debug the Webapi to see what happens next there... thank you so much for now!
– Thiago
@LINQ, you were right my friend. The problem was in the service... as much as my model had DECLARED that Datarecepcao was of the type ? Datetime, the service was wrong to include complaining of null value. WA: Step any value and change to null directly in the Controller. (per hour)... THANK YOU!
– Thiago