Skip to content

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

Closed
@jy95

Description

@jy95

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.good first issueIssues that are suitable for first-time contributors.querystringIssues and PRs related to the built-in querystring module.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions