【问题标题】:how to return HTTP status code in Open FaaS?如何在 Open FaaS 中返回 HTTP 状态码?
【发布时间】:2018-11-23 22:29:54
【问题描述】:

部署在 openfaas 中的函数如何向调用者返回不同的 HTTP 状态码?像 4xx 代码。

根据文档,看门狗将处理 stdoutstderr 以获取 http 状态 200 或 5xx。

有没有办法改变 400、409 等状态?我正在使用 faas-cli 下载的 csharp 模板

【问题讨论】:

    标签: openfaas


    【解决方案1】:

    你不能,如此处所述https://github.com/openfaas/faas-netes/issues/147

    建议的解决方案是返回一个带有状态码的payload,并在接收端解析payload。

    【讨论】:

    • 我将不得不探索 of-watchdog http 模式。
    【解决方案2】:

    默认 python 模板似乎返回 200,即使您 system.exit(1) 或抛出异常也是如此。我假设其他旧模板的行为类似,这是经典看门狗的预期。

    但是,如果您使用的语言模板supports running the new of-watchdog, I think it can return non-200 codes, like 400, 404, etc.,您可以使用 faas-cli 下载这些模板,它们来自 openfaas-incubator 项目和社区。​​p>

    我刚刚确认使用来自 openfaas-incubator 的 python3-http 语言/模板,csharp-httprequest from distantcam 可以返回非 200 代码,如 400、404 等。

    这将列出可用的模板:

    faas-cli template store list
    

    要安装我测试过的 csharp 模板:

    faas-cli template store pull distantcam/csharp-httprequest
    

    可以像这样轻松修改 csharp-httprequest 的 OOTB 处理程序以返回 400:

    using Microsoft.AspNetCore.Http;
    using System.IO;
    using System.Threading.Tasks;
    
    namespace Function
    {
        public class FunctionHandler
        {
            public async Task<(int, string)> Handle(HttpRequest request)
            {
                var reader = new StreamReader(request.Body);
                var input = await reader.ReadToEndAsync();
    
                return (400, $"Hello! Your input was {input}");
            }
        }
    }
    

    【讨论】:

    • of-watchdog 中您必须使用哪种模式? README 中唯一可用的信息是流式传输模式,并表示这种行为是不可能的。
    • 我在使用这个模板时不必更改模式:github.com/distantcam/csharp-httprequest-template
    • 你使用的是哪个模板@BtcSources?
    • 我正在使用python-http 模板和http 模式。似乎工作。不能确切地说,因为我正在修改一些东西来改变默认行为。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多