我正在尝试使用 Node URL 和 URLSearchParams API 来简化 URL 构建。我正在使用它来构建这样的 URL:
https://<ye olde oauth host>/oauth/authorize?response_type=code&client_id=<my client id>&redirect_uri=https://localhost:4200&scopes=scope1%20scope2
但是,我下面的代码正在创建这样的 URL:
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
我的理解是 URLSearchParams API 将对字符串进行百分比编码,但是如果我不希望它们被编码,比如 URL,该怎么办?这是我的代码:
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);
我不想要
redirect_uri
的原因进行百分比编码是因为接收端的 OAuth API 不知道如何解析它。有没有办法使用 URLSearchParams 来阻止它编码?
最佳答案
我有一个类似的问题。一些百分比符号使我无法发送 token (无法正确解析 token )。最后我只用了decodeURIComponent(url)
就在发送请求之前。
关于javascript - 使用 URLSearchParams 防止百分比编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53951058/