I just follow the example to wrap my Express application. There is a "app.delete" handler in my app. When DELETE request is being served, there is strange error thrown out from aws-serverless-express, as shown below
ERROR: aws-serverless-express connection error
{ Error: socket hang up at createHangUpError (_http_client.js:253:15) at Socket.socketOnEnd (_http_client.js:345:23)
My code is very simple:
app.delete('/user', routes.user.remove);
module.exports.remove = [
(request, response) => {
let user = request.body;
console.log('remove user='+JSON.stringify(user));
db.users.delete(user.username, (err) => {
if (err) {
response.status(500).send('internal error');
}
else {
response.json({ username: user.username });
}
});
}
];
Just for testing purpose, if I replace the app.delete handler with app.patch, and invoke the call using HTTP PATCH method, with exactly same request parameters, the call is handled correctly. And this is my workaround for the moment.
Pls help. Thank you
I have the same issue when I send a body with DELETE requests.
There is an interesting discussion about whether a body is allowed here: https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
The conclusion seems to be that the body should be allowed and ignored. In any event, causing a 502 doesn't seem like the best possible approach.
@cliffgibson please confirm: did the DELETE request have a body? I will try to reproduce.
@cliffgibson DELETE request is not supported response body. Please use POST or PUT method instead of DELETE If you really want to get a response body.
@novemberde the issue is not that we want to receive a body. We want to be able to send a body without getting a 502 error. The RFC allows for that.
We're not doing anything funny in this library checking for a body on DELETE and causing a 502. I'll look into it and see where the problem lies. I assume this doesn't happen with a standard Express app (i.e. can you run it locally and DELETE works?(
@brettstack Yes, DELETE works on Express app.
here's code.
const app = require("express")();
app.delete("/", (req, res, next) => {
res.status(200).json({
message: "Receive body on DELETE Method!"
});
});
app.listen(3000, () => console.log("Running!"));
Encountered the same issue, and it's especially infuriating when you have the same code executed by PUT and DELETE but DELETE crashes without a single understandable error message. This should be fixed, the RFC HTTP1.1 spec doesn't forbid the use or request body on DELETE commands, and as such - express supports it.
I don't have the time to confirm this, but does API Gateway support request body on DELETE?
Have there been any updates on this?
Also:
I don't have the time to confirm this, but does API Gateway support request body on DELETE?
I've just confirmed that it does. Somewhere along the line it seems that this module is dropping the body for DELETE requests
This error happened to me and the reason was that I was sending a body with the DELETE request. After I took it the error went away. For me this fixes the problem, but as I understand, sending a body along with a DELETE method is not wrong.
You can try the PR. I haven't confirmed it though.
I tested @HotelCalifornia's PR and it worked. Until it gets merged in, I was able to get around this by manually setting the content length if its not:
exports.handler = (event, context) => {
// fix to deal with body being dropped on DELETE requests
if (event.httpMethod === 'DELETE' && event.body && (!event.headers || !event.headers['Conent-Length'])) {
console.log('Adding content length for DELETE body');
event.headers = event.headers || {};
event.headers['Content-Length'] = Buffer.byteLength(event.body);
}
return awsServerlessExpress.proxy(server, event, context);
}
I'm working on integ tests right now and will merge that PR after they're in. Is the issue with API Gateway not sending the Content-Length header for DELETE requests with a body?
I think the issue is just that API gateway ignores the body in a DELETE request unless the Content-Length header is specified
I don't think that's what's happening. event.body is always coming through if you provide it in the request, right? My understanding is Express/Node.js http is throwing 502 when body exists with no content-length header in the request. So my assumption here is that API Gateway isn't including a content-length header for DELETE requests even when they have a body.
Your CR looks good. I just made a couple comments.
You're right, that does sound more correct. If I'm honest, it's been a few weeks since I've thought about this problem 馃槄
Closing in favor of #175. This will be out in the next release
Most helpful comment
@novemberde the issue is not that we want to receive a body. We want to be able to send a body without getting a 502 error. The RFC allows for that.