Srini V
Srini V

Reputation: 65

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

Upvotes: 3

Views: 27628

Answers (4)

Ahmad
Ahmad

Reputation: 782

Just in Addition of Xueli Chen Answer I tested it in .net core2.2

The Student Model

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


In Postman ,set "Content-Type" to "application/json" in the Headers and also set [Produces("Application/json")] on controller Side
In Controller , If you dont use [FromQuery] in the parameter of action it will also work and i think it just Extra type Casting and it i will use resources for conversion also. If we dont use it will also work and i change of parameter why are you getting List of students in parameter it will aslo word without it.

[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
    [Produces("application/json")]
    [HttpPost]
    public void PostStudent(Student students)
    {
          // you can get data here from students objects
    }
}

Upvotes: 0

Xueli Chen
Xueli Chen

Reputation: 12725

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.

Upvotes: 3

Gabriel Luci
Gabriel Luci

Reputation: 40998

I assume you're using this in a GET (otherwise there is no point in doing this). I will also assume you want the students parameter of the URL to be set to that array.

You have to URL encode the whole string. You can use an online encoder like https://www.urlencoder.org to do it. That will take this:

[{"id":"1","names":["john","james"]},{"id":"2","names":["peter","harry"]}]

And turn it into this:

%5B%7B%22id%22%3A%221%22%2C%22names%22%3A%5B%22john%22%2C%22james%22%5D%7D%2C%7B%22id%22%3A%222%22%2C%22names%22%3A%5B%22peter%22%2C%22harry%22%5D%7D%5D

So that's what you'd put in your URL:

http://example.com?students=%5B%7B%22id%22%3A%221%22%2C%22names%22%3A%5B%22john%22%2C%22james%22%5D%7D%2C%7B%22id%22%3A%222%22%2C%22names%22%3A%5B%22peter%22%2C%22harry%22%5D%7D%5D

But I hope this shows why no one does this :) It's messy and it can get really long really fast.

The answer by @RomanMarusyk is a little more civilized, but the signature of your controller needs to match it: you would accept a List<Student> (or whatever your student class is called).

Ideally, JSON objects like this should be in the body of the request, and you would use an HTTP method that accepts a body (like POST or PUT).

Upvotes: 1

Roman Marusyk
Roman Marusyk

Reputation: 24609

Try this one:

?students[0][id]=1&students[0][names][0]=john&students[0][names][1]=james&students[1][id]=2&students[1][names][0]=peter&students[1][names][1]=harry

Upvotes: 0

Related Questions