Send 2 or more Raw Body Parameters Post Request with Postman

11,496

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.

Share:
11,496
Saeid
Author by

Saeid

Updated on August 16, 2022

Comments

  • Saeid
    Saeid over 1 year

    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?

  • Saeid
    Saeid almost 6 years
    It's not Post By Raw Body