Phenomenal One
Phenomenal One

Reputation: 2587

How to get unencoded Query Parameter in Node.js

I am doing Google Oath2 Implementation. For a particular authorization_code, I was constantly getting invalid_grant. I checked the value and found that the query string value was encoded.

Here is an example:

const parser = require('url');
url ='http://example.com/test?param=4%2F12'
const q = parser.parse(url, true).query
console.log(q)

My output here is

{ param: '4/12' }

I want my output to be

{ param: '4%2F12' }

as the correct auth code is a string with value 4%2F12. How do I implement this?. There may be many manual ways to do this. Anything that needs a minimalistic code effort would be appreciated. Thanks in advance!

Upvotes: 0

Views: 191

Answers (1)

Laode Muhammad Al Fatih
Laode Muhammad Al Fatih

Reputation: 4610

Simple. Just encode again the param using encodeURIComponent.

Example:

console.log(encodeURIComponent("4/12")) // Output: 4%2F12

Upvotes: 3

Related Questions