Express Application in AWS Lambda has base64 encoded body every single time - node.js

I'm making post requests to an AWS Api gateway with a lambda proxy integration pointing to a lambda function. This lambda function is a wrapper for an existing express application. I could decode the body myself and set the body to be appropriately formatted, but I don't have to do this when I run the express server manually. I'm essentially trying to avoid adding middleware that checks if it's a lambda function, then setting the body to be the decoded JSON of the body. I thought this would be an easy fix (and it probably is), but I'm not sure how to do it.
All requests are made from Postman with x-www-form-urlencoded.
I've tried messing around with some of the body parsers, binarymimetypes, doing application/json on postman, using awsserverless event context middleware.
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(bodyParser.json());
app.use((request, response, next) => {
console.log("GOT BODY!");
console.log(request.body);
next();
});
The above code prints the base64 encoded value if the app is on aws lambda and prints the correct JSON value if running locally.

Related

Body parsers behaving weirdly

I am trying to develop an app with few APIs, the issue I am facing here is when I use
app.use(bodyParser.urlencoded({ extended: false }));
as my middleware, it doesnt intake the requests i send through postman (all the request sent through postman goes with empty body idk why. And it takes all the requests sent via html form.
On the other hand, if I use
app.use(express.json({extended: false}))
as my middleware to parse json objects, it takes all the requests from postman but doesnt take requests from my browser form. Can anyone explain whats happening here?
In order for express to be able to parse both JSON request payloads and simple form-data requests, you simply need to setup both of the mentioned middlewares (note that express.json() does not have an extended option):
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
See the docs for more information:
https://expressjs.com/en/api.html#express.json
https://expressjs.com/en/api.html#express.urlencoded

When using Put Method in a Express JS Api , Do I need to use body parser?

In the code snippet below, do I need to use the urlencodedParser like I do in Post methods.
app.put('/api/provider/:id', urlencodedParser, function (req, res) {
}
body-parser parses the body of the request into req.body, which you'll likely need for your put middleware. body-parser now comes built into Express (as of v4.16.0 - below assumes you have an updated version).
The easiest implementation is to use express.json and express.urlencoded (used in body-parser) in all requests, using app.use, so that you don't have to worry about it in your middleware. Here is how npx express-generator $APP_NAME will set it up for you:
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
NOTE: You'll need to set extended to true if you are expecting nested objects in your requests.

Request Entity Too Large

My Express application is returning "Request Entity Too Large" on a file upload of only a 125kb PNG file.
I have configured the body parser middleware as such:
app.use(bodyParser.urlencoded({
limit: '5mb',
type:'*/x-www-form-urlencoded',
extended: true
}));
according to the documentation. No matter how high I set the limit, or what combination of options, I always get the same result. I am using Express 4.13.3 and body-parser 1.15.2.
What am I doing wrong?
Embarrassingly, I had accidentally pointed my route handler to the wrong Express Router instance. Once I was pointed to the correct handler, Multer picked up the multi-part POST correctly.
Interestingly, when testing with Postman, it will send an array of files, even if you only select one, which is why request.file was undefined, but request.files contained the correct value.

Can't POST (and parse) params to app.post() in express.js

I checked this question but for some reasons the solution given is not working for me. In my express.js I have:
...
bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
...
Run test using POSTman
app.post('/upload', function(req, res) {
console.log(req.body) // undefined
console.log(req.params) // undefined
})
and the result:
So both body & params are empty. Any suggestions?
The reason the solution in the link you provided doesn't work is because that version of body-parser is out of date and doesn't include form-data parsing anymore (and it used to be bundled with express).
With that being said, based on your screenshot, it looks like you are sending data of type multipart/form-data(you can check this in the request headers) to your server and your code sample only shows middleware that handles urlencoded and json data types.
You need to add middleware that handles that data type. The latest body parser says (https://github.com/expressjs/body-parser):
This does not handle multipart bodies, due to their complex and
typically large nature. For multipart bodies, you may be interested in
the following modules:
busboy and connect-busboy
multiparty and connect-multiparty
formidable
multer
So check out one of the above parsers. I use busboy and it works great.

Making a proxy with request and express in node

I'm trying to make a proxy with the express and request module. This post shows how to easily pipe() the request to the response:
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
//app.use(express.multipart());
app.use('/api', function(req, res) {
var url =proxyUrl + req.url;
req.pipe(request(url)).pipe(res);
});
It works very well for all GET request. But on a POST request it fails. Can't figure out what is happening because it only runs into a timeout. The POST request works on the proxy.
What am I doing wrong? Is there any changes that I can debug the request? I have tried the following, but the file is empty:
req.pipe(fs.createWriteStream("test.txt"));
We are also experiencing same issues, its because express.json(). After disabling json parser, it work ok. But that not the idle solution as you might need json parser.

Resources