Node.js querystring.encode() Function
The querystring.encode() method is used to produce a URL query string from the given object that contains the key-value pairs. The method iterates through the object’s own properties to generate the query string.
It can serialize a single or an array of strings, numbers, and boolean. Any other types of values are coerced to empty strings.
During serializing, the UTF-8 encoding format is used to encode any character that requires percent-encoding. To encode using an alternative character encoding, the encodeURIComponent option has to be specified.
querystring.encode( obj, sep, eq, options )
Parameters: This function accepts four parameters as mentioned above and described below:
- obj: Object that has to be serialized into the URL query string.
- sep: String that specifies the substring used to delimit the key and value pairs in the query string. The default value is “&”.
- eq: String that specifies the substring used to delimit keys and values in the query string. The default value is “=”.
- options: It is an Object which can be used to modify the behavior of the method. It has the following parameters:
- encodeURIComponent: It is a function that would be used to convert URL-unsafe characters to percent-encoding in the query string. The default value is querystring.escape().
Return Value: It returns a String that contains the URL query produced from the given object.