I have json error like this:
{"errors": {"Listmodel": ["Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[sistap_api.Models.AmbarHareketRequestModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Listmodel', line 1, position 13."]}, "status": 400, "title": "One or more validation errors occurred.", "traceId": "00-3cd8a186e7bcd84f93c8c18bef8ca908-f4fbb2109d75914d-00", "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1"}
This is my fetch in React native:
const ambarHareketOnay = async () => {
const convertedData = route.params.data.map(item => ({
KullaniciId: parseInt(route.params.userId),
RuloSiparisDetayId: parseInt(item.RuloSiparisDetayID),
EskiLokasyonAmbar: parseInt(item.AmbarID),
EskiLokasyonKoridor: parseInt(item.AmbarID),
YeniLokasyonAmbar: parseInt(route.params.ambarId),
YeniLokasyonKoridor: parseInt(secilenKoridorId),
}));
console.log("data: ",convertedData);
try {
fetch(global.apiDomain + 'api/ambarHareket', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
body: JSON.stringify({ Listmodel: convertedData }),
})`
This is output console.log("data: ",convertedData);
:
data: [{"EskiLokasyonAmbar": 1, "EskiLokasyonKoridor": 1, "KullaniciId": 1, "RuloSiparisDetayId": 50328, "YeniLokasyonAmbar": 1, "YeniLokasyonKoridor": 1048}, {"EskiLokasyonAmbar": 1, "EskiLokasyonKoridor": 1, "KullaniciId": 1, "RuloSiparisDetayId": 50330, "YeniLokasyonAmbar": 1, "YeniLokasyonKoridor": 1048}]
This is the ASP.NET Core Web API endpoint:
[HttpPost]
public IActionResult ambarHareketOnay ([FromBody] List<AmbarHareketRequestModel> Listmodel)
public class AmbarHareketRequestModel
{
public int KullaniciId { get; set; }
public int RuloSiparisDetayId { get; set; }
public int EskiLokasyonAmbar { get; set; }
public int EskiLokasyonKoridor { get; set; }
public int YeniLokasyonAmbar { get; set; }
public int YeniLokasyonKoridor { get; set; }
}
What is the mistake I'm making?
Thanks
You're wapping convertedData
in another object. While convertedData
itself is a list, { Listmodel: convertedData }
is an object. When you JSON.stringify
that, you get an object, which lines up with the error message you're getting.
To fix this, don't wrap it.
body: JSON.stringify(convertedData),
You don't need to specify the name of the C# parameter ("Listmodel") in JavaScript because the C# parameter has the [FromBody]
attribute. It's equal to the entire JSON payload.