Could someone please tell me how i can test this function :
[RoutePrefix("service")]
public class TestControler : ApiController
{
[Route("function-route")]
[HttpPost]
public HttpResponseMessage Testfunction(TestData t_testData )
{
...
HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
return httpResponseMessage;
}
}
public class TestData
{
public byte[] PreviewImage { get; set; }
public byte[] FileAsByteArray { get; set; }
}
We have Swagger enabled via :
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
//Using AttributeRoutes
config.MapHttpAttributeRoutes();
//Swagger
config.EnableSwagger(c =>{
c.SingleApiVersion("v1", "My Test API");
}).EnableSwaggerUi();
appBuilder.UseWebApi(config);
}
I really do not know how to test this API via swagger, or postman, or curl. The problem ist the byte[], how to send this? Does anyone how to test these kind of api?
Or, if there is another way to send any file (pdf, txt, docx, ...) combined with any picture (jpg, png, ...) without byte[], i would be glad to hear.
byte array in CAn unsigned char can contain a value from 0 to 255, which is the value of a byte. In this example, we are declaring 3 arrays – arr1, arr2, and arr3, arr1 is initialising with decimal elements, arr2 is initialising with octal numbers and arr3 is initialising with hexadecimal numbers.
Arrays. fill(). This method assigns the required byte value to the byte array in Java. The two parameters required for java.
In the byte stream model, a file is just a stream of bytes, with no record boundaries. Newline characters written to the stream appear in the external file. If the file is opened in binary mode, any newline characters previously written to the file are visible on input.
A consecutive sequence of variables of the data type byte, in computer programming, is known as a byte array. An array is one of the most basic data structures, and a byte is the smallest standard scalar type in most programming languages.
I would base64 encode the byte arrays and change your model properties from byte[]
to string
. That should allow you to post these strings to your controller where you can convert them back to byte arrays.
To encode as base64 string use string byteString = Convert.ToBase64String(byteArray)
and convert back using byte[] byteArray = Convert.FromBase64String(byteString)
.
Your updated code might look something like:
public class TestData
{
public string PreviewImage { get; set; }
public string FileAsByteArray { get; set; }
}
[Route("function-route")]
[HttpPost]
public HttpResponseMessage Testfunction(TestData t_testData)
{
// convert base64 string to byte[]
byte[] preview = Convert.FromBase64String(t_testData.PreviewImage);
byte[] file = Convert.FromBase64String(t_testData.FileAsByteArray);
...
HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
return httpResponseMessage;
}
To test using postman you can use javascript functions atob()
and btoa()
to convert a byte array to a base 64 encoded string and vice versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With