Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting form data with file results in "The input was not valid" error

I'm using Postman to test a .Net Core 2.1 Web API. I started with this and it worked -- I was able to send a file from Postman to my Web API.

Web.API

[HttpPost("uploads")]
public IActionResult FileUpload(IFormFile myfile)
{
   ...
   return Ok();
}

However, I need to pass some additional form data along with the file. Based on other posts that I've seen, I tried the following:

[HttpPost("uploads")]
public IActionResult FileUpload(UploadFile data)
{
    ...
    return Ok();
}

public class UploadFile {
   public IFormFile myfile {get; set;}
   public bool forceOverwrite {get; set;}
}

Now posting from Postman results in "The input was not valid". I've done some searching and can't seem to find anybody that has gotten something like this working.

Update

I also tried the following; however, the additional value always comes in false -- even when sent as true.

public IActionResult FileUpload(IFormFile myfile, bool forceOverwrite)

Update 2

enter image description here

enter image description here

Here is what I added in Postman, and also how it appears in Fiddler. (P.S. You can ignore the fact that the route in the POST doesn't match the route listed in my code -- I chose to simplify things for the post in regards to naming.

like image 428
RHarris Avatar asked Jul 01 '18 01:07

RHarris


1 Answers

For binding FormData in .NET Core Web API, you will need to add FromForm attribute like below

    [HttpPost("uploads")]
    public IActionResult FileUpload([FromForm]UploadFile data)
    {
        return Ok();
    }
like image 176
Edward Avatar answered Dec 02 '22 23:12

Edward