400 (Bad Request) while sending json to .net 6 Web API

Viewed 51

I am trying to send data from winform datagridview to webapi, so i convert datagridview to json. While sending json it failed to POST data and send this error:

'Failed to POST data: (BadRequest): {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title": "One or more validation errors occurred.","status":400,"traceId":"00-fb84f3c1c1802a481cbe7aba7ef73193-80be6fe9c57344d2-00","errors":{"$":["The JSON value could not be converted to LabDataApi.Models.LabData. Path: $ | LineNumber: 0 | BytePositionInLine: 421."]}}'

Codes:

public class LabData
    {
        public int SerialNumber { get; set; }
        public string LabParameterName { get; set; }
        public decimal LabValue { get; set; }
        public DateTime LabDate { get; set; }
    }

private void button2_Click(object sender, EventArgs e)
            {
                var url = "https://localhost:7248/api/Lab";
                dataGridView1.DataSource = LabResult.GetLabData();     
                var table = JsonConvert.SerializeObject(dataGridView1.DataSource);
                ApiSender apiSender = new ApiSender();
                apiSender.POSTData(table, url);
            }

public class ApiSender
    {
        private static HttpClient _httpClient = new HttpClient();

        public bool POSTData(object json, string url)
        {
            using (var content = new StringContent(JsonConvert.SerializeObject(json), System.Text.Encoding.UTF8, "application/json"))
            {
                HttpResponseMessage result = _httpClient.PostAsync(url, content).Result;
                if (result.StatusCode == System.Net.HttpStatusCode.Created)
                    return true;
                string returnValue = result.Content.ReadAsStringAsync().Result;
                throw new Exception($"Failed to POST data: ({result.StatusCode}): {returnValue}");
            }
        }
    }

Receiving Json data format

[{"SerialNumber":1,"LabParameterName":"abc","LabValue":7.80,"LabDate":"2001-11-16T10:10:00"},{"SerialNumber":2,"LabParameterName":"xyz","LabValue":10.00,"LabDate":"2001-11-16T10:10:00"},{"SerialNumber":3,"LabParameterName":"qq","LabValue":5.00,"LabDate":"2001-03-16T10:10:00"},{"SerialNumber":18,"LabParameterName":"cbc","LabValue":200.0,"LabDate":"2001-11-16T10:10:00"}]

enter image description here

2 Answers
The JSON value could not be converted to LabDataApi.Models.LabData

Well, you are trying to convert an array of LabData into the LabData model. Update the API to receive IEnumerable<LabData>, or update the client to send multiple POST requests (for each LabData entry (not recommended)).

At least that's the answer to the question you've asked - it looks like you have cut off the end of the exception, perhaps you omitted some essential details by mistake?

There is an issue with your objects definitions when converting to json. Also, you'll want to get it as a List or an Array.

Your serialization/deserialization will work better with these objects.

public class LabDatas
{
    public LabData[] Value { get; set; }
}

//or if you want list
public class LabDatas
{
    public List<LabData> Value { get; set; }
}

public class LabData
{
    public int SerialNumber { get; set; }
    public string LabParameterName { get; set; }
    public float LabValue { get; set; }
    public DateTime LabDate { get; set; }
}
Related