I have a model like this.
public class ClientDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public IFormFile ProfilePicture { get; set; }
}
And API controller like this:
[HttpPost]
public async Task<OperationResult> Post([FromForm] ClientDto c)
{
ImageHandler handler = new ImageHandler(env);
Client client = new Client()
{
Address = c.Address,
FirstName = c.FirstName,
LastName = c.LastName,
PhoneNumber = c.PhoneNumber,
PhotoPath = await handler.UploadFile(c.ProfilePicture)
};
return await clientRepository.AddClient(client);
}
Now if I make a Post
request with postman after including a file with ProfilePicture
as key (under Postman >> Body >> form-data section ), model binding works as expected. The api controller receives PofilePicture
and rest of the properties null
. But if I include json string for other properties like this ( under Postman >> Body >> raw section)
{
"c":{
"FirstName": "XYZ",
"LastName":"ABX"
}
}
OR
{
"FirstName": "XYZ",
"LastName":"ABX"
}
model binding no longer works. How do I hit the api controller with postman if i want to provide all properties?
You're mixing data sent to the controller - it's confused.
In your request with the image the content type is multipart/form-data
as the body contains a file and potentially other form data fields. In the request with JSON the content type is application/json
and you're not sending any file data.
You have to decide to either only send the JSON or the form data.
In this case, since you're sending a file, you need to use multipart/form-data
and just set the missing fields in the form fields from Postman ( see how to set Postman form-data parameters here ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.