0

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.

3 Answers 3

2

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

thanks for the suggestion, it's really helpful, and yeah, i use automapper in the service class like: _mapper.Map(classDto, class); but for some reason i cant use .ForMember method in this block of code, maybe I should create a class just for maps and use CreateMap(class, classDto).ForMember(x => x.File, o => o.ignore()) ? Thanks for the help again man, and sorry if my questions looks stupid, im learning :)
0

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

1 Comment

file is good, im not having trouble with that, the point is ClassDto bannerDto is not receiving any values from Form Data, its always null. Based on what you said I tried to select a .txt json file with the parameters like: { "Id": 1, "Comunicacao": {"ImagemDesktop":"Teste"} } but that doesnt worke either

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.