Friday, April 8, 2022

[SOLVED] Test zip file upload using HTTP PUT method using the curl

Issue

I am trying to upload the zip file to a folder using the put method. Below is my put method which accepts the zip file:

 [HttpPut("ImportFile")]
    [Consumes("multipart/form-data")]
    public async Task<IActionResult> AddFileToFolder(IFormFile file)
    {
        
        string fileExtension;

        if (file == null)
        {
            return BadRequest();
        }

         //The helper method that upload file to folder "C:\test" folder
         await Helper.UploadFile(file);
       


        return Ok();
    }
}

When I test this using the postman it is working fine. But when I test it using the curl like below:

C:\test> curl --insecure PUT -T "C:\test\Test_1.zip" "https://localhost:7098/TestController/ImportFile"

I get the error of "Could not resolve host: PUT"

I also get error when I try used the below command with curl:

curl --insecure https://localhost:7098/TestController/ImportFile --upload-file "C:/test/Test_1.zip" In this case I get these errors:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-d2c03ced5ba843a959c42a6fdb049122-c32240f288eac9b8-00","errors":{"file":["The file field is required."]}}


Solution

you could click the circle in the picture and you could see the curl with postman curl

Codes in api controller:

[HttpPut("ImportFile")]
        [Consumes("multipart/form-data")]
        public IActionResult upload(IFormFile file)
        {
            var a = HttpContext.Request;
            return Ok();
        }

enter image description here

Curl:

curl --location --request PUT 'https://localhost:44323/WeatherForecast/ImportFile' \
--form 'file=@"/C:/Users/ruikaif/Desktop/x.xml.txt"'


Answered By - Ruikai Feng
Answer Checked By - Marilyn (WPSolving Volunteer)