Home > front end >  400 (Bad Request) while sending json to .net 6 Web API
400 (Bad Request) while sending json to .net 6 Web API

Time:09-16

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:

One or more validation errors occurred.","status":400,"traceId":"00-76f7972fcf00afc89a7f164ca37ee7ab-5198bf8e30719871-00","errors":{"$":["The JSON value could not be converted to LabDataApi.Models.LabData

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"}]

CodePudding user response:

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?

CodePudding user response:

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