Reputation: 113
I developed an endpoint in .net core that takes parameters from FromBody as a byte array. This endpoint converts the incoming file into an image and saves it in the folder in the file directory.
I know that the byte array value is a file. But this byte array value comes from outside. I want to get this byte array with the endpoint I wrote. So while testing, I tried all kinds of methods from postman. I put debug but it doesn't go to the breakpoint section.
I want to test this endpoint How can I do?
Endpoint:
[HttpPost("ConvertByteToImageFile")]
public async Task<IActionResult> ConvertByteTomageFile([FromBody] byte[] data)
{
//Resime çevirir.
var imageData = GetDataToImage(data);
try
{
//Convert Image.
SaveImageToFile(imageData);
return Ok($"Image saved successfully at");
}
catch (Exception ex)
{
return StatusCode(500, $"An error occurred: {ex.Message}");
}
}
[NonAction]
public Image GetDataToImage(byte[] byteData)
{
using (MemoryStream ms = new(byteData))
{
Image image = Image.FromStream(ms);
return image;
}
}
[NonAction]
public void SaveImageToFile(Image image)
{
try
{
var imageFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "CameraFiles");
var imageName = Guid.NewGuid().ToString(); // Benzersiz bir isim oluştur
// Save image
var imagePath = Path.Combine(imageFolderPath, imageName);
image.Save(imagePath);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Upvotes: 0
Views: 20
Reputation: 381
You need to pass a raw json string to the service w/ the byte[] in the form of a int32 array. For example if your service takes an object w/ a parameter FileBytes:Byte[] then you'd provide the json string {"FileBytes": [5,12,13,200,...,10]}. This should be the format that a restful service needs to properly convert to the byte[] of the application
Upvotes: 0