javascript - 使用 URLSearchParams 防止百分比编码

标签 javascript node.js angular typescript oauth

我正在尝试使用 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/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property '$store' of null

javascript - 有没有办法将js数组中的元素连接起来,但让最后一个分隔符不同?

angular - 如何根据选定的下拉值过滤 *ngFor 结果

Angular 7 : How to access query string in URL from an iframe?

javascript - Angular:模板重新呈现数组,而其长度不变

Javascript : more clever way to filter thousands of row against thousands of values

javascript - 如何在express js中执行res.redirect时传递 header

php - 从 php 启动 Node 似乎可以让 php 保持运行?

javascript - HTML 表单将数据 POST 到 MySQL

javascript - Node.js 错误 : EISDIR, 打开错误