azure 使用八位字节流的HTTP触发器

gfttwv5a  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(113)

是否可以创建使用application/octet-stream而不是application/json的Azure Functions HTTP触发器?
我目前有一个application/json的触发器。

jqjz2hbq

jqjz2hbq1#

为了将HTTP触发器中的数据作为流进行流式传输:
根据这个Github文档,***你可以编辑你的函数。HTTP触发器的json文件,数据类型为二进制或流,以流式传输数据***如下:-

function.json代码:-

{

"generatedBy": "Microsoft.NET.Sdk.Functions.Generator-4.1.1",

"configurationSource": "attributes",

"bindings": [

{

"type": "httpTrigger",

"direction": "in",

"dataType": "stream",

"route": "myroute",

"methods": [

"post"

],

"authLevel": "function",

"name": "req"

}

],

"disabled": false,

"scriptFile": "../bin/FunctionApp9.dll",

"entryPoint": "MyFunction.RunAsync"

}

另外,根据Andy的SO Thread回答,通过引用Andy的代码,我在运行HTTP触发器后在浏览器中得到了以下输出:-Here response。ContentType设置为“application/json-data-stream”;

***Andy's Code:-*函数。cs:-

using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using
Microsoft.Azure.WebJobs.Extensions.Http; using
Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using
Newtonsoft.Json; using System.Collections.Generic;

namespace FunctionApp10 {
    public class Function1
    {
        private async IAsyncEnumerable<string> GetDataAsync()
        {
            for (var i = 0; i < 100; ++i)
            {
                yield return "{\"hello\":\"world\"}";
            }
        }

        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var response = req.HttpContext.Response;

            await using var sw = new StreamWriter(response.Body);
            await foreach (var msg in GetDataAsync())
            {
                await sw.WriteLineAsync(msg);
            }

            await sw.FlushAsync();

            response.StatusCode = 200;
            response.ContentType = "application/json-data-stream";

            return new EmptyResult();
        }
    }
  }

输出:-触发器已执行:-

在浏览器中得到如下流式响应:-

相关问题