Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node /Express, How to configure bodyParser to use raw on certain routes only

I'm not quite sure if I am approaching this correctly, but my problem is, that I receive usually json data at my API endpoint, then I am creating a model instance from the json req.body data...and I am using MEAN stack..

The problem that I now have is, that one of the fields of my model is of mongoose-long datatype with 18 positions. However, the bodyParser parses the number and rounds the las 3 positions of the number. I use it for the _id field, so I am getting duplicate key errors.

I construct and send the data manually via Python for testing, and I use _ast_lib_eval(document) to strip all strings quotes from my constructed json document. Therefore, somehow the json bodyParser is getting the body and takes the number but cuts off (or rounds) the number fields because I guess, it does not support such long numbers natively...I could use another bodyParser for certain routes maybe... but I'm not sure how to configure that.

What is the best approach now? Do I have to use another parser and therefore send my data as plain text or raw for all api endpoints? That would mean I have to redo everything so that it fits the parser, right? The usual format is json....

or can I somehow get the "raw" data from the req.body before it is getting parsed from the bodyparser? That would be the best way to manually get the number out right.. but I don't know if there's a method to do that...

like image 933
Marc_L Avatar asked Jun 18 '18 07:06

Marc_L


Video Answer


1 Answers

Based on specification you are able to specific custom bodyparser on all endpoints

app.post('/some/special/route', bodyParser.json({verify:function(req,res,buf){req.rawBody=buf}}))
app.use(bodyParser.json())

Issue on gh

like image 176
Juraj Kocan Avatar answered Sep 22 '22 15:09

Juraj Kocan