Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Percent Encoding with URLSearchParams

I am trying to use the Node URL and URLSearchParams APIs to make URL building a bit simpler. I'm using it to build a URL like this:

https://<ye olde oauth host>/oauth/authorize?response_type=code&client_id=<my client id>&redirect_uri=https://localhost:4200&scopes=scope1%20scope2

However, my code below is creating the URL like this:

https://<ye olde oauth host>/oauth/authorize?response_type=code&client_id=<my client id>&redirect_uri=https%3A%2F%2Flocalhost%3A4200&scopes=scope1%20scope2

It's my understanding that the URLSearchParams API will percent-encode strings, but what if I don't want them to be encoded, like URLs? Here is my code:

const loginURL = new URL('https://<ye olde oauth host>');
    url.pathname = 'oauth/authorize';
    url.search = new URLSearchParams({
      response_type: 'code',
      client_id: '<my client id>'
    }).toString();
loginURL.searchParams.append('redirect_uri', redirectURI);
loginURL.searchParams.append('scopes', scopes);

The reason I don't want the redirect_uri to be percent encoded is because the OAuth API on the receiving end doesn't know how to parse it. Is there any way using the URLSearchParams to stop it from encoding?

like image 527
skylerl Avatar asked Nov 16 '22 18:11

skylerl


1 Answers

I've had a similar issue. Some percentage symbols were causing me trouble to send a token (the token couldn't be parsed correctly). At the end I just used decodeURIComponent(url) just before the request was sent.

like image 168
yavorbel Avatar answered Nov 23 '22 23:11

yavorbel