HTTP headers and status sent with response body when context.res.body is undefined
See original GitHub issueInvestigative 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:
- 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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
context.res = bodyValue; // res is interpreted as 200 with body bodyValue
context.res = { body: bodyValue, headers: ..., status: ... } // res is fully specified object
Code assumes that a context.res object without a “body” property = bodyValue
Something like this has existed for awhile,
e.g. Slightly different, but very similar: https://stackoverflow.com/questions/41709910/azure-functions-nodejs-http-response-renders-as-xml-rather-than-http-response/41709911#41709911