I have turn to the API, which apparently accepts parameters for a redirecting request as one of the parameters. I prefer use the URL
object to create requests. To add and encode parameters, I use searchParams
. But searchParams encodes, among other things, the parentheses (
and )
, which eventually leads to request error.
What i try to do:
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)
And what i got:
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
What i should got:
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
My solution:
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
So my question goes like this: how to pass parentheses in the URL or how to make this code better?