Saeid
Saeid

Reputation: 13582

Send 2 or more Raw Body Parameters Post Request with Postman

I have an ASP.NET Core 2.0 API Method:

[HttpPost("api/days")]
GetDays([FromBody] DateTime startTime, [FromBody]DateTime endTime)
{

}

And I tried to send a Post request with Postman, but there is a problem, the parameters always have default values.

Here is my Post request looks like:

enter image description here

Result: Not Worked; Both parameters in API method get the default values.

If I change my API params to:

[HttpPost("api/days")]
GetDays([FromBody] Inputparam param)
{

}

public class Inputparam
{
    public DateTime startTime { get; set; }
    public DateTime endTime { get; set; }
}

That's worked perfectly!

But I wanna to send parameters directly and not inside wrapper object.

So, I came back with first API method and then I tried:

enter image description here

Result: Not Worked; Both parameters in API method get the default values.

And This one:

enter image description here

Result: Not Worked perfectly; Just first parameter (startTime) set it and second parameter still have default value.

And This one:

enter image description here

Result: Not Worked; Both parameters in API method get the default values.

I also tried [FromForm] instead of [FromBody] in API, nothings changed.

If I don't use [FromBody] in api and send the request via x-www-form-urlencoded that's worked perfectly.

But I need send a raw body with JSon.

How could I sent 2 different parameters as a raw body json?

Any idea? Where is the problem?

Upvotes: 2

Views: 17302

Answers (2)

Debashish Saha
Debashish Saha

Reputation: 336

[FromUri] can do the Job.For your reference Microsoft Documentation

public class DateTimes
{
public datetime startdate { get; set; } 
public datetime enddate { get; set; }
}

GetDays([FromBody] DateTimes _date)
{
  //Controller action
}

URI:

api/values/?startdate=2018-06-01&endate=2018-07-01

Upvotes: 0

Saeid
Saeid

Reputation: 13582

I found that it's just one [FromBody] is allowed in API, and that makes sense. So probably the answer is: There is no way to have 2 or more parameters with [FromBody] attribute.

Upvotes: 5

Related Questions