Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

body-parser - extended option (qs vs querystring)

In the current version of body-parser, the extended option when using bodyParser.urlencoded() is now required. In the README, it explains:

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true).

[...]

Defaults to true, but using the default has been deprecated. Please research into the difference between qs and querystring and choose the appropriate setting.

I couldn't find any helpful or specific information on this. I only found a deprecated node-querystring.

Should this option just always be true?

like image 219
c.. Avatar asked Mar 20 '15 20:03

c..


People also ask

What is difference between qs querystring?

To be simple, querystring cannot parse nested object. Instead, it will be presented in [] format. While qs can be parsed in nested object.

What is extended in body-parser?

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The “extended” syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded.

What is qs package?

qs: Quick Serialization of R Objects This package includes code from the 'zstd' library owned by Facebook, Inc. and created by Yann Collet; the 'lz4' library created and owned by Yann Collet; xxHash library created and owned by Yann Collet; and code derived from the 'Blosc' library created and owned by Francesc Alted.

What is the use of Bodyparser in express?

Express body-parser is an npm module used to process data sent in an HTTP request body. It provides four express middleware for parsing JSON, Text, URL-encoded, and raw data sets over an HTTP request body.


1 Answers

The reason for this message is that body-parser is about to change default value for extended from true to false.

Extended protocol uses qs library to parse x-www-form-urlencoded data. The main advantage of qs is that it uses very powerful serialization/deserialization algorithm, capable of serializing any json-like data structure.

But web-browsers don't normally use this protocol, because x-www-form-urlencoded was designed to serialize flat html forms. Though, it may come in handy if you're going to send rich data structures using ajax.

querystring library` provides basic serialization/deserialization algorithm, the one used by all web-browsers to serialize form data. This basic algorithm is significantly simpler than extended one, but limited to flat data structures.

Both algorithms work exactly the same with flat data.

Now, when you know pros and cons of both algorithms, it's up to you to decide which one suits your application better.

like image 103
Leonid Beschastny Avatar answered Sep 28 '22 03:09

Leonid Beschastny