Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 2.1 "POST" an IFormFile using Postman - the application completed without reading the entire request body

I'm working on a dotnet core WebAPI 2.1 and I can't find a way of sending to into the Body an image.

The controller looks like this:

    [HttpPost("api/image")]
    public IActionResult Post([FromBody]IFormFile file)
    {
        var filePath = Path.GetTempFileName();
        if (file.Length > 0)
        {
            return Ok();
        }
        return BadRequest();
    }

And this is my Postman call: enter image description here

This call is never finishing as the Kestrel is failing enter image description here

I've already tried to use Consumes

[Consumes("application/json", "application/json-patch+json", "multipart/from-data")]

Also in postman, I have set Content-Type to multipart/form-data

like image 363
rm -rf . Avatar asked Jul 19 '18 13:07

rm -rf .


2 Answers

I found the solution for .Net Core 2.1/2.2 here

POST multiple files from POSTMAN

POST single file from POSTMAN

    [HttpPost]
    public async Task<IActionResult> UploadSingleFile([FromForm(Name = "file")] IFormFile file)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);
        var IDsList = new IDsList();

        try
        {
            var id = SaveFile(file);
            IDsList.Files.Add(new IDsList.FileInfo() { id = id, fileName = file.FileName });
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

        return Content(JsonConvert.SerializeObject(IDsList), "application/json");
    }

    [HttpPost("UploadFiles")]
    public async Task<IActionResult> UploadFiles([FromForm(Name = "files")] ICollection<IFormFile> files)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        //var files = Request.Form.Files?.GetFiles("files");
        String message = String.Empty;
        int filesCounter = 0;
        var IDsList = new IDsList();

        foreach (var file in files)
        {
            if (file.Length == 0)
                message = message + $"errorDescription {file.FileName}\n";

            try
            {
                var id = SaveFile(file);
                IDsList.Files.Add(new IDsList.FileInfo() { id = id, fileName = file.FileName });
                filesCounter++;
            }
            catch(Exception ex)
            {
                message = $"{message}{ex.Message}\n";
            }
        }

        IDsList.message = $"Amount: {filesCounter}.\n{message}";

        return Content(JsonConvert.SerializeObject(IDsList), "application/json");
    }
like image 75
Valery Spiridonov Avatar answered Sep 21 '22 06:09

Valery Spiridonov


Seems like there is an error problem with Postman macOS application. When I'm using the postman chrome extension everything works as expected.

MacOS app MacOS app

Chrome extension enter image description here

like image 44
rm -rf . Avatar answered Sep 20 '22 06:09

rm -rf .