ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.58k stars 1.33k forks source link

GET request with unencoded queryparams #1645

Open zero2411 opened 2 years ago

zero2411 commented 2 years ago

I'm using NodeJS with Superagent to connect to an API. The URL should look like this:

https://example.com/api/schedule/default/events?query={"jobSourceIds":["614999492641e60d523e1f51"],"status":["ready","notstarted"]}

I've tested this URL in Postman and it works fine.

But Superagent transforms this to:

https://example.com/api/schedule/default/events?query=%7B%22jobSourceIds%22:[%22614999492641e60d523e1f51%22],%22status%22:[%22ready%22,%22notstarted%22]%7D

The query-params are encoded which I don't want since my API can't process it. The API ignores the params this way.

The queryParams are provided as a string in the right form.

I've tried to set the Content-type and request.type to JSON, but no luck.

Any suggestions on how to get this right?

var request = superagent[method.toLowerCase()](baseUrl + path);
request.set("Accept", "application/json");
request.set("Cache-Control", "no-cache");
// request.set('Content-Type', 'application/json')
request.set('Content-Type', 'application/x-www-form-urlencoded')

var headers = createHeaders(method, path);
  Object.keys(headers).forEach(function (k) {
  request.set(k, headers[k]);
});

request.proxyWrapper = function (proxy) {
if (proxy == undefined) return this;
  return this.proxy(proxy);
};

console.log(queryParams)        // query={"jobSourceIds":["614999492641e60d523e1f51"],"status":["ready","notstarted"]}
console.log(typeof queryParams) // string
return request.send(payload).query(queryParams).proxyWrapper(proxy).end(responseHandler);