0

I have written a Web API in ASP.NET Core, for which I need to pass 2 parameters; of them one is a string with grade, the other is of type list of studentInfo as shown here:

[HttpPost]
[Route("UpdateActiveStudents")]
public Response UpdateActiveStudents(string grade, [FromBody] List<StudentsInfo> lststudents)
{
        try
        {
            // My Logic
        }
        catch(Exception ex)
        {
            resp.flag = false;
            resp.message = ex.Message;
        }

        return resp;
}

To test this API, I used ARC (Advanced Rest Client). I passed the data as like this in a POST request:

{
    "grade": "B",
    "lststudents": [
                       { "StudentName": "abcdef", "RollNo": "user1"}, 
                       { "StudentName": "abcdef", "RollNo": "user1"}
                   ]
}

It throws a HTTP 400 status error with the following message :

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[SchoolHub.Model.StudentList]' 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 'lststudents', line 2, position 13.

I'm unaware of this exception.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Srikanth Reddy
  • 447
  • 1
  • 8
  • 23

3 Answers3

0

In Web API, general parameter binding rules for POST method are as follows -

Query string -> Primitive type
Request body -> Complex type

Now if you want to use POST method with Mixed parameters i.e in your case you are passing primitive (string) and complex (List), Web API will get the grade parameter from query string and student parameter from the request body.

Possible solutions to try -

  1. In the ARC request it seems you are passing grade in request body instead of as a query string parameter. Try passing grade as a query string parameter.
  2. Also add a class viz. StudentInfoRequest to wrap List<StudentsInfo> lststudents and then use StudentInfoRequest object to pass as parameter to UpdateActiveStudents method.
  3. You dont need to mention [FromBody] in UpdateActiveStudents method as by default complex parameters are read from the request body by Web API.

Hope this helps!

Aparna Gadgil
  • 410
  • 4
  • 9
0

You have this issue because you are not sending the data in the format at which ASP.Net Web API expects. ASP.net Web API needs some special format when dealing with a value like string and value type (int, bool, etc) parameter are marked with FromBody attribute.

Just remove FromBody it will work. For better understanding go with this link.

Why do we have to specify FromBody and FromUri?

ANJYR
  • 2,583
  • 6
  • 39
  • 60
0

You must add [ApiController] in the controller level. Code is as follows

[ApiController]

[Route("[controller]")]

public class studentController : ControllerBase
{
    [HttpPost]
    [Route("UpdateActiveStudents")]
    public Response UpdateActiveStudents(string grade, List<StudentsInfo> 
                    lststudents)
    {
        //code
    }  
}
David Buck
  • 3,752
  • 35
  • 31
  • 35