Javascript 具有 beta 运行时的 Azure 函数不会 return 错误 HTTP 响应的响应正文

Javascript Azure function with beta runtime does not return response body for error HTTP responses

使用 Javascript Beta 运行时的 Azure 函数,我遇到了一个问题,如果函数 returns 成功响应 (200, 201),则响应正文从 context.res 返回正确返回。

但是如果 HTTP 状态设置为 400 或 404,无论在函数中设置什么响应主体,客户端都会在响应主体中得到标准响应,如 "Bad Request"。

我什至可以在自动生成的 java 脚本函数上重现此内容。

代码(标准自动生成代码)

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request 
body"
        };
    }
    context.done();
};

使用 v1 运行时,如果在查询或请求正文中未指定名称,HTTP 状态将设置为 400 并且响应正文正确包含 "Please pass a name on the query string or in the request body".

使用 beta 运行时,HTTP 状态设置为 400,但响应正文仅包含 "Bad request"

测试版运行时是否需要以不同的方式返回响应正文?

代码没问题,这是测试版运行时的已知问题。

请参阅Azure Functions Runtime 2.0.11888 Announcement

HttpTrigger does not return response content properly. For example following code will return "Bad Request" instead of "Please pass a name on the query string or in the request body": return new BadRequestObjectResult("Please pass a name on the query string or in the request body");

If this is blocker, you can use previous version by setting FUNCTIONS_EXTENSION_VERSION to 2.0.11857-alpha(in Application settings).

更新

此问题已在 2.0.11933 中得到解决。