Search code examples
c#asp.net-corepostmanform-data

How to create a POST with form-data for an API endpoint that accepts a list of files inside a list of objects?


I am trying to create a request in postman to upload files to a dotnet core endpoint that expects the following shape:

public class MyDto
{
   public DateTime Time { get; set; }
   public List<Logs> Files { get; set; } = null!;
   public class Logs
   {
       public ServiceEnum Service { get; set; }
       public List<IFormFile> LogFiles { get; set; }
   }
}

Function signature of action:

[HttpPost("{​id}​/log")]
public async Task<ActionResult> SaveLogs([FromRoute] string id, [FromForm] MyDto myDto)

My postman request looks like this: Screen shot of postman setup with attempted api request

When I try debugging this I can see that myDto.Files[0].Service is set yet, myDto.Files[0].LogFiles in null.

I was expecting that uploading multiple files would be mapped into myDto.Files[0].LogFiles from the request. I might be formatting the keys in the request wrong but I have tried multiple different key formats at this point.


Solution

  • I test with other simple and complex types, they can use square brackets and post successfully(Except for IFormFile). Maybe it is by design in Postman. Any way, no matter what type it is. You can always post like xx.xx.xx.

    Change your post data like below:

    enter image description here