Testing file upload with Swagger in ASP.Net core
Let’s assume you have an API which is used for uploading image to the backend and you wanted to test it when the UI is still in development.
[HttpPost("UploadImage")]
public ActionResult UploadImage()
{
IFormFile file = HttpContext.Request.Form.Files[0];
_logger.LogInformation(file.FileName); // we can put rest of upload logic here.
return Ok();
}
So first thing is you do is to check swagger UI and to your surprise you didn’t see the way to upload the file in swagger UI.
So then how will you test.
there are couple of ways by which we can achieve this.
In this tutorial, we are going to look at them.
Let’ start by making some changes in API first.
Accept IFormFile as Parameter
first way is to change the API to accept IFormFile as parameter.
[HttpPost("UploadImage")]
public ActionResult UploadImage(IFormFile file)
{
_logger.LogInformation(file.FileName);// we can put rest of upload logic here.
return Ok();
}
Now ran the app again and see the swagger UI.
Now we can see upload file button and we can test now.
Accept Multiple files as parameter
Change the code to accept multiple files.
[HttpPost("UploadMultipleImages")]
public ActionResult UploadMultipleImages(IFormFile[] files)
{
foreach (var item in files)
{
_logger.LogInformation("file uploaded : " + item.FileName);
}
// we can rest of upload logic here. return Ok();
}
Now run the app
We can now add multiple files and upload them together.
The second way..
In case we don’t want to change the API signature and keep it in the same way.
[HttpPost("UploadImage")]
public ActionResult UploadImage()
It is still possible to test with swagger but we have to add some code explicitly.
Let’ start by adding class called “FileUploadFilter”
and add the following code.
also add the following code to ConfigureServices method
services.AddSwaggerGen(options =>
{
options.OperationFilter<FileUploadFilter>();
});
Now let’s run the app and see the swagger UI.
We can see the possibility to add the file in request from swagger UI.
Source code can be found at here
Summary
Testing file uploads with swagger UI can be useful when you want to test your API without writing any other client to send the file.
Hope you like it..
Happy reading and Keep learning..!
FileUpload Icon by https://www.flaticon.com/