Search code examples
c#restasp.net-core-3.1form-data

Accepting form-data in ASP.NET Core 3.1 REST API


I'm trying to create a REST service that is able to accept form-data. I am using the [FromForm] attribute but the object's properties are filled with null values.

I have the following endpoint:

[HttpPost("upload")]
public ActionResult<UploadResponse> Upload([FromForm] UploadRequest req)
{
    ...do something
}

UploadRequest.cs:

public class UploadRequest
{
    public IFormFile File { get; set; }
    public string Format { get; set; }
    public string CaseId { get; set; }
    public string Description { get; set; }
}

I post form-data using Postman like this:

enter image description here

The request parameter is filled with null values, though:

enter image description here

I have tried matching the case of parameters, adding and removing "multipart/form-data" header but nothing works for me. What am I missing?


Solution

  • After having a rough time seeking for a solution I found out that I was missing boundary in Content-Type header. So the Content-Type header should look as following: multipart/form-data; boundary=<calculated when request is sent>.