【发布时间】:2018-11-23 22:29:54
【问题描述】:
部署在 openfaas 中的函数如何向调用者返回不同的 HTTP 状态码?像 4xx 代码。
根据文档,看门狗将处理 stdout 或 stderr 以获取 http 状态 200 或 5xx。
有没有办法改变 400、409 等状态?我正在使用 faas-cli 下载的 csharp 模板
【问题讨论】:
标签: openfaas
部署在 openfaas 中的函数如何向调用者返回不同的 HTTP 状态码?像 4xx 代码。
根据文档,看门狗将处理 stdout 或 stderr 以获取 http 状态 200 或 5xx。
有没有办法改变 400、409 等状态?我正在使用 faas-cli 下载的 csharp 模板
【问题讨论】:
标签: openfaas
你不能,如此处所述https://github.com/openfaas/faas-netes/issues/147
建议的解决方案是返回一个带有状态码的payload,并在接收端解析payload。
【讨论】:
默认 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 中唯一可用的信息是流式传输模式,并表示这种行为是不可能的。
python-http 模板和http 模式。似乎工作。不能确切地说,因为我正在修改一些东西来改变默认行为。