Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to bypass querystring.stringify with empty Object ? #12234

Closed
jy95 opened this issue Apr 5, 2017 · 7 comments
Closed

Any way to bypass querystring.stringify with empty Object ? #12234

jy95 opened this issue Apr 5, 2017 · 7 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. querystring Issues and PRs related to the built-in querystring module. question Issues that look for answers.

Comments

@jy95
Copy link
Contributor

jy95 commented Apr 5, 2017

I am working currently on a project that requires to send custom GET HTTP requests.

I am using the default querystring builder (https://nodejs.org/api/querystring.html)

const querystring = require('querystring');

The problem is for Object (probably also for empty array) such as

{ "extendTypes":{} }  

is serialiazed as :

extendTypes=

The expected result :

extendTypes={}

or its URI encoded version :

extendTypes%3D%7B%7D

So , how can I ever hope to do that ? Any way to bypass this default behalvior ?

Here is my full code if you want :

function generateGetRequest(dataMap, url) {

    let queryParams = {};
    let uriParams = {};

    for (let [key, value] of dataMap) {

        // if value is an object or an array
        if (value instanceof Object || value instanceof Array) {
            uriParams[key] = value;
        } else {
            // param working for superagent
            queryParams[key] = value;
        }

    }
    let queryParamsUri = querystring.stringify(uriParams);
    console.log(queryParamsUri);
    let finalUrl = url + ( (Object.keys(uriParams).length > 0) ? "?" + queryParamsUri : "");
}
  • Version: 6.9.4
  • Platform: Windows 10
@jy95 jy95 changed the title NodeJs querystring.stringify not working with Object Any way to bypass querystring.stringify with empty Object Apr 5, 2017
@jy95 jy95 changed the title Any way to bypass querystring.stringify with empty Object Any way to bypass querystring.stringify with empty Object ? Apr 5, 2017
@mscdex mscdex added querystring Issues and PRs related to the built-in querystring module. question Issues that look for answers. labels Apr 5, 2017
@aqrln
Copy link
Contributor

aqrln commented Apr 5, 2017

https://github.com/nodejs/help, https://gitter.im/nodejs/node and #node.js channel on IRC are better places to ask such questions. FWIW, you probably want to pass a JSON string as the value of extendTypes.

I believe the behavior of querystring.stringify() is absolutely correct and it produces the result I'd expect it to, but I agree that the documentation may be improved.

@jy95
Copy link
Contributor Author

jy95 commented Apr 5, 2017

I agree with you about the documentation. As least , why not provide a list of value that will not be seriliazed ? I don't think there are so many cases
For example :

undefined
null
''
""
{}
[]

@joyeecheung joyeecheung added doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. labels Apr 10, 2017
@joyeecheung
Copy link
Member

Yeah it's more like a doc issue. Though I think this should be a list of values that will be serialized instead of values that will not be serialized? (logic is in

node/lib/querystring.js

Lines 219 to 268 in cfc8422

function stringifyPrimitive(v) {
if (typeof v === 'string')
return v;
if (typeof v === 'number' && isFinite(v))
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
}
function stringify(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var encode = QueryString.escape;
if (options && typeof options.encodeURIComponent === 'function') {
encode = options.encodeURIComponent;
}
if (obj !== null && typeof obj === 'object') {
var keys = Object.keys(obj);
var len = keys.length;
var flast = len - 1;
var fields = '';
for (var i = 0; i < len; ++i) {
var k = keys[i];
var v = obj[k];
var ks = encode(stringifyPrimitive(k)) + eq;
if (Array.isArray(v)) {
var vlen = v.length;
var vlast = vlen - 1;
for (var j = 0; j < vlen; ++j) {
fields += ks + encode(stringifyPrimitive(v[j]));
if (j < vlast)
fields += sep;
}
if (vlen && i < flast)
fields += sep;
} else {
fields += ks + encode(stringifyPrimitive(v));
if (i < flast)
fields += sep;
}
}
return fields;
}
return '';
}
, so it's basically just: strings, finite numbers, booleans, and arrays of them).

Labeld as good first contribution. Feel free to send in a doc PR.

@shubheksha
Copy link
Contributor

Can I take this up?

@addaleax
Copy link
Member

@shubheksha Go for it! Let us know if you need any help. :)

@jy95
Copy link
Contributor Author

jy95 commented Apr 10, 2017

@shubheksha thanks.

@refack
Copy link
Contributor

refack commented Jul 19, 2017

Closed by #12313

@refack refack closed this as completed Jul 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. querystring Issues and PRs related to the built-in querystring module. question Issues that look for answers.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants