【问题标题】:Best way to pass parentheses in URL?在 URL 中传递括号的最佳方法?
【发布时间】:2021-09-22 14:44:24
【问题描述】:

我已经求助于 API,它显然接受重定向请求的参数作为参数之一。我更喜欢使用URL 对象来创建请求。要添加和编码参数,我使用searchParams。但 searchParams 对括号 () 等进行编码,最终导致请求错误。

我想做什么:

const country = 'us'
const language = 'en'
const anchor = 0
const consumerChannelId = 'd9a5bc42-4b9c-4976-858a-f159cf99c647'

const count = 24

const remoteSearch= `/product_feed/rollup_threads/v2?filter=marketplace(${country})&filter=language(${language})&anchor=${anchor}&consumerChannelId=${consumerChannelId}&count=${count}`

const url = new URL('https://example.xyz/v1')
url.searchParams.append('queryid', 'products')
url.searchParams.append('country', country)
url.searchParams.append('language', language)
url.searchParams.append('endpoint', remoteSearch)

我得到了什么:

https://example.xyz/v1?queryid=products&country=us&language=en&endpoint=%2Fproduct_feed%2Frollup_threads%2Fv2%3Ffilter%3Dmarketplace%28us%29%26filter%3Dlanguage%28en%29%26anchor%3D0%26consumerChannelId%3Dd9a5bc42-4b9c-4976-858a-f159cf99c647%26count%3D24

我应该得到什么:

https://example.xyz/v1?queryid=products&country=us&language=en&endpoint=en&endpoint=%2Fproduct_feed%2Frollup_threads%2Fv2%3Ffilter%3Dmarketplace(us)%26filter%3Dlanguage(en)%26anchor%3D0%26consumerChannelId%3Dd9a5bc42-4b9c-4976-858a-f159cf99c647%26count%3D24

我的解决方案:

const country = 'us'
const language = 'en'
const anchor = 0
const consumerChannelId = 'd9a5bc42-4b9c-4976-858a-f159cf99c647'

const count = 24

const remoteSearch = `/product_feed/rollup_threads/v2?filter=marketplace(${country})&filter=language(${language})&anchor=${anchor}&consumerChannelId=${consumerChannelId}&count=${count}`
const endpoint = encodeURIComponent(remoteSearch)

const url = new URL('https://example.xyz/v1')
url.searchParams.append('queryid', 'products')
url.searchParams.append('country', country)
url.searchParams.append('language', language)
url.searchParams.append('endpoint', language)

const correctURL = url.href + '&endpoint=' + endpoint

所以我的问题是这样的:如何在 URL 中传递括号或如何使这段代码更好?

【问题讨论】:

  • 括号“()”可以用在URL的查询部分。将它们编码为“%28”和“%29”是允许的,但不是必须的。

标签: javascript url urlsearchparams


【解决方案1】:

括号“()”可用于 URL 的查询部分。将它们编码为“%28”和“%29”是允许的,但不是必须的。


您可以使用YOURURL.replace(/\(/g, "%28").replace(/\)/g, "%29") 将所有括号替换为正确的 URL 编码,

const country = 'us'
const language = 'en'
const anchor = 0
const consumerChannelId = 'd9a5bc42-4b9c-4976-858a-f159cf99c647'

const count = 24

const remoteSearch= `/product_feed/rollup_threads/v2?filter=marketplace(${country})&filter=language(${language})&anchor=${anchor}&consumerChannelId=${consumerChannelId}&count=${count}`
  .replace(/\(/g, "%28").replace(/\)/g, "%29"); // use regex to replace

const url = new URL('https://example.xyz/v1')
url.searchParams.append('queryid', 'products')
url.searchParams.append('country', country)
url.searchParams.append('language', language)
url.searchParams.append('endpoint', remoteSearch)

console.log(url);

【讨论】:

  • 我不明白为什么我们需要替换 remoteSearch 中的括号,如果 searchParams 无论如何都会对它们进行编码。在这种情况下,我们得到(例如):marketplace%2528us%2529% 看起来我们正在添加一个新的包装器。如果没有替换,searchParams 将它们编码为:marketplace%28us%29 但我需要以这种方式将它们传递给 URL:marketplace(us) 远程 API 不接受编码括号
【解决方案2】:

我建议使用 encodeURIComponent() 它的内置函数,但在您的情况下,您可以创建一个单独的函数来执行此操作可能没有用

function encodeNonURIComponent(url) {
  return encodeURIComponent(url).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}

【讨论】:

  • 也许我表达不正确,但我只是不需要编码括号。出于这些目的,我当前的解决方案使用 encodeURIComponent。但是我必须使用字符串连接添加带有值 remoteSearch 的端点参数(因为 searchParams 方法对括号进行编码 - 我不需要它)
猜你喜欢
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2012-01-10
  • 2015-01-28
相关资源
最近更新 更多