Azure-functions-host: HTTP headers and status sent with response body when context.res.body is undefined

Created on 11 Jan 2018  路  5Comments  路  Source: Azure/azure-functions-host

Investigative information

Please provide the following:

  • Timestamp: 2018-01-10
  • Function App version (1.0 or 2.0-beta): 2.0-beta
  • Function App name: N/A
  • Function name(s) (as appropriate): N/A
  • Invocation ID: N/A
  • Region: N/A

Repro steps

Provide the steps required to reproduce the problem:

  1. Create an HTTP triggered function that sets headers and status but not body on response object:
module.exports = (ctx, req) => {
    ctx.res = {
        headers: {
            'Access-Control-Allow-Origin': req.headers.origin,
            // ... Set other Access-Control headers
        },
        status: 204,
        //body: '' <-- Adding this results in expected behaviour
    };
    ctx.done();
};

Expected behavior

Response has status 204 and Access-Control headers

Actual behavior

Response has status 200, no Access-Control headers and the following JSON encoded body:

{
    "headers": { "Access-Control-Allow-Origin": "..." },
    "status": 204
}

If I add a body property to ctx.res with an empty string value, I get the expected result.

Related information

I see this when running locally using the preview version of the cli on OSX.

1.x 2.0 breaking bug lang-js needs-investigation

All 5 comments

Weird. I wonder if we somehow have a code path that if there is no body returned just assumes there was no response. I'll check into this.

Nice, two repros is helpful.

Could be the root cause of https://github.com/Azure/azure-functions-host/issues/2578 as well, where using v2.1.2 of node-fetch appears to return the wrong body (some bad assumption is being made).

Root cause of this issue is https://github.com/Azure/azure-functions-host/issues/997

To summarize, an http response is created in two ways:

  1. context.res = bodyValue; // res is interpreted as 200 with body bodyValue
  2. context.res = { body: bodyValue, headers: ..., status: ... } // res is fully specified object

Code assumes that a context.res object without a "body" property = bodyValue

Was this page helpful?
0 / 5 - 0 ratings