Lucas Lucena
Lucas Lucena

Reputation: 60

How to Send Object Values in PostMan From FromData Calling a Web Api .net Method

I have a post API method in my web API net core application thats has a Dto Class with the follow parameters:

public class ClassDto
    {
        public int Id { get; set; }
        public Comunicacao Comunicacao { get; set; }
    } 

Comunicacao Class:

public class Comunicacao
    {
        public int Id { get; set; }
        public string Name { get; set; }
    } 

API Action(route has been setted correctly):

[HttpPost]
        public async Task<IActionResult> Add([FromForm]ClassDto bannerDto, IFormFile imgDesktop)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var result = await _banner.Add(classDto, imgDesktop);

                    return Ok(new { message = result.messageReturning, result.classDto });
                }
                else
                {
                    return BadRequest(ModelState);
                }
            }
            catch (Exception ex)
            {

                return BadRequest(ex.ToLogString(Environment.StackTrace));
            }
        }

So my question is, how can I send in Postman using FormData passing Comunicacao Object Values? Because when I sent "Id", it works fine, but I can't find a way to send objects!

what i have tried yet

I cannot use FromBody because as you all can see I'm sending a File as well.

Upvotes: 0

Views: 1266

Answers (3)

Lucas Lucena
Lucas Lucena

Reputation: 60

Finally got it. Have to use the object.property!

enter image description here

Upvotes: 2

RAHUL S R
RAHUL S R

Reputation: 1579

put the iformfile object inside your dto making it a single dto method then ur endpoint will look like public async Task Add([FromForm]ClassDto bannerDto) if you have put the iformfile outside cos of automapper then u can use [ignore] attribute over the property

sorry wanted to put this as comment for the previous answer but i am typing from mobile phone so ...meh

Upvotes: 1

RAHUL S R
RAHUL S R

Reputation: 1579

you can set body type to form-data and then in key value pair side just select file instead of text for your file upload

Postman screenshot

Upvotes: 0

Related Questions