dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

body-parser/express not working with missing request body on GET #149

Closed AlexBHarley closed 4 years ago

AlexBHarley commented 4 years ago

Just leaving this here so I can come back to it later and try fix it.

I've got two routes set up currently

app.use(bodyParser.json())

app.get('/', (req, res) => {
  const {headers} = req;
  console.log(headers);
  res.status(200).send();
});

app.post('/', (req, res) => {
  const {body} = req;
  console.log(body);
  res.status(200).send();
})

The get fails whether I use cURL or window.fetch on the GET / route.

My work around has been

// app.use(bodyParser.json())
app.post('/', bodyParser.json(), (req, res) => {
AlexBHarley commented 4 years ago

Sorry, this was me being an idiot, spent around 2 hours debugging and digging through various libraries.

I had set the Content-Type header on my GET request instead of Accept. Another library I'm using (https://github.com/netlify/netlify-lambda) was rewriting the body based on that header and encoding it into base64, that was why the bodyParser library was blowing up.