question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTTP headers and status sent with response body when context.res.body is undefined

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mhoegercommented, Apr 26, 2018

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

1reaction
securityvoidcommented, Jan 12, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

res.body is undefined in express GET request
The body of the HTTP response can be created by many different methods, for example res.send or res.write in combination with res.end ....
Read more >
Response: body property - Web APIs | MDN
The body read-only property of the Response interface is a ReadableStream of the body contents.
Read more >
Express 4.x - API Reference
It parses incoming request payloads into a Buffer and is based on body-parser. Returns middleware that parses all bodies as a Buffer and...
Read more >
How can req.headers and req.body be undefined in node.js?
I think Express initializes req.headers and req.body even if they're empty or not defined in the incoming request.
Read more >
HTTP | Node.js v20.5.1 Documentation
Emitted when the request has been sent. More specifically, this event is emitted when the last segment of the response headers and body...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found