Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass JSON object as parameter from Postman to ASP.NET WEB API

I want to pass json object as a query string parameter (not from body) to ASP.NET Core Web API url from postman. Kindly let me know how to pass? below the sample JSOB object structure :

here, 'names's is string array

"students":[
      {
         "id":"1",
         "names":["john", "james"]
      },
      {
         "id":"2",
         "names":["peter", "harry"]
     }
]
like image 960
Srini V Avatar asked Feb 03 '26 10:02

Srini V


1 Answers

This is a demo I made , you could refer to

In Postman , remember to set "Content-Type" to "application/json" in the Headers, otherwise you might get a error - 415 Unsupported MediaType.

https://localhost:44388/api/student/?students[0].id=1&students[0].name[0]=john&students[0].name[1]=james&students[1].id=2&students[1].name[0]=peter&students[1].name[1]=harry

The Student Model

 public class Student
{
    public int Id { get; set; }
    public string[] Name { get; set; }
}

In Controller , do not forget add [FromQuery] in the parameter of action.

[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
    [HttpPost]
    public void PostStudent([FromQuery]List<Student> students)
    {
    }
}

The screenshot of students parameter

enter image description here

As Gabriel Luci said ,the Json object is best passed in the body of request.

like image 82
Xueli Chen Avatar answered Feb 05 '26 01:02

Xueli Chen