ASP Core WebApi Test File Upload using Postman
Thanks to @rmjoia's comment I got it working! Here is what I had to do in Postman:
How can I send file to list - .net core web api - postman
You need to change the request body with dot pattern like this:
Then you need to add [FromForm] attribute to the controller input parameter.
Also note that the variable names in the postman and controller must match.
[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages ([FromForm]IList<PostRepoRequest> repositoryItems)
With these changes, you will be able to get the request correctly:
How to test asp.net controller to upload multiple files in postman
Your code is working fine no need to use [] with the key just add multiple fields with the same key and postman will converts them to array.
ASPNetCore - Uploading a file through REST
As you've noticed already, using binary file
option in Postman/Insomnia doesn't work the standard way. There are three different ways to upload file via RESTful API, and you have to choose one.
I've included code snippets that read the uploaded file contents to a string and output it -- try sending a text file, and you should get the contents of the file in the 200 response.
Form-data upload
This is the most popular/well-known upload method formatting the data you send as a set of key/value pairs. You normally need to specify Content-Type
to multipart/form-data
in the request, and then use [FromForm]
attribute in MVC to bind values to variables. Also, you can use the built-in IFormFile
class to access the file uploaded.
[HttpPost]
public async Task<IActionResult> PostFormData([FromForm] IFormFile file)
{
using (var sr = new StreamReader(file.OpenReadStream()))
{
var content = await sr.ReadToEndAsync();
return Ok(content);
}
}
Body upload
You can send body in the format that MVC understands, e.g. JSON, and embed the file inside it. Normally, the file contents would be encoded using Base64 or other encoding to prevent character encoding/decoding issues, especially if you are sending images or binary data. E.g.
{
"file": "MTIz"
}
And then specify [FromBody]
inside your controller, and use class for model deserialization.
[HttpPost]
public IActionResult PostBody([FromBody] UploadModel uploadModel)
{
var bytes = Convert.FromBase64String(uploadModel.File);
var decodedString = Encoding.UTF8.GetString(bytes);
return Ok(decodedString);
}
// ...
public class UploadModel
{
public string File { get; set; }
}
When using large and non-text files, the JSON request becomes clunky and hard to read though.
Binary file
The key point here is that your file is the whole request. The request doesn't contain any additional info to help MVC to bind values to variables in your code. Therefore, to access the file, you need to read Body
in the Request
.
[HttpPost]
public async Task<IActionResult> PostBinary()
{
using (var sr = new StreamReader(Request.Body))
{
var body = await sr.ReadToEndAsync();
return Ok(body);
}
}
Note: the example reads Body
as string
. You may want to use Stream
or byte[]
in your application to avoid file data encoding issues.
Is it impossible to mix fileupload and raw json in asp core web api?
In case anyone is still wondering about this question, I followed the suggestion i got from @IhorKostrov and created a multipart/form-data request. The solution can be found: using jsonData as a key value to send mixed data in asp: Does not update the database with the values.
Related Topics
C# Percentage Calculation Explanation
How to Get the List of Properties of a Class
How to Determine If .Net Core Is Installed
How to Efficiently Remove a Query String by Key from a Url
Ef Migration for Changing Data Type of Columns
Browser Back Button Navigates to Login Page When User Is Still Logged In
Unicode Characters Replace from String Using C#
C# Double - Tostring() Formatting With Two Decimal Places But No Rounding
C# Windows Form Application for Employee Management
How to Print the Elements With Text Value That Contains in a List Selenium C#
Ssh.Net Sftp Get a List of Directories and Files Recursively
Asp Core Webapi Test File Upload Using Postman
Update Value in Datatable from Another Datatable
Using Newtonsoft to Deserialize a Date Stamp That Might Consist Only of a Year
How to Display Values Only Upto 2 Decimal Places