Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transmitting Binary Data via OpenAPI on Azure Functions
public static class BinaryDataHttpTrigger
{
[FunctionName(nameof(BinaryDataHttpTrigger.RunByteArray))]
[OpenApiOperation(operationId: "run.bytearray", tags: new[] { "bytearray" }, ...)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiRequestBody(contentType: "text/plain", bodyType: typeof(byte[]), ...)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "image/png", bodyType: typeof(byte[]), ...)]
public static async Task<IActionResult> RunByteArray(
[HttpTrigger(AuthorizationLevel.Function, "POST", Route = "bytearray")] HttpRequest req,
ILogger log)
{
var payload = default(string);
using (var reader = new StreamReader(req.Body))
{
payload = await reader.ReadToEndAsync().ConfigureAwait(false);
}
var content = Convert.FromBase64String(payload);
var result = new FileContentResult(content, "image/png");
return result;
}
}
paths:
/bytearray:
post:
requestBody:
content:
text/plain:
schema:
type: string
format: base64
responses:
'200':
content:
image/png:
schema:
type: string
format: base64
public static class BinaryDataHttpTrigger
{
[FunctionName(nameof(BinaryDataHttpTrigger.RunMultiPartFormData))]
[OpenApiOperation(operationId: "run.multipart.formdata", tags: new[] { "multipartformdata" }, ...)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiRequestBody(contentType: "multipart/form-data", bodyType: typeof(MultiPartFormDataModel), ...)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "image/png", bodyType: typeof(byte[]), ...)]
public static async Task<IActionResult> RunMultiPartFormData(
[HttpTrigger(AuthorizationLevel.Function, "POST", Route = "form/multipart")] HttpRequest req,
ILogger log)
{
var files = req.Form.Files;
var file = files[0];
var content = default(byte[]);
using (var ms = new MemoryStream())
{
await file.CopyToAsync(ms).ConfigureAwait(false);
content = ms.ToArray();
}
var result = new FileContentResult(content, "image/png");
return result;
}
}
public class MultiPartFormDataModel
{
public int Id { get; set; }
public string Description { get; set; }
public byte[] Image { get; set; }
}
paths:
/form/multipart:
post:
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/multiPartFormDataModel'
responses:
'200':
content:
image/png:
schema:
type: string
format: base64
components:
schemas:
multiPartFormDataModel:
type: object
properties:
id:
type: integer
format: int32
description:
type: string
image:
type: string
format: base64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment