skip to Main Content

I have a request which is like "xxxx.com/api/xxx?url=wwww.xxx.com?id=abc"
With this kind of URL at the end ‘=abc’ value is missing. I know the reason because there is no & before the second query but the thing is I can not change the current value. How can I get the missing value?

2

Answers


  1. Your url is not correct, multiple query parameters should be used with & not extra ?
    Change it like this: xxxx.com/api/xxx?url=wwww.xxx.com&id=abc

    Login or Signup to reply.
  2. Gather them inside an array

    console.log('searchParams', searchParams);
    
    const p = searchParams?.url ? searchParams.url.toString().split('?') : [];
    console.log('p', p);
    
    const values: any = [];
    
    for (let i = 0; i < p.length; i++) {
      if (p[i].indexOf('=') > -1) {
        values.push({ key: p[i].split('=')[0], value: p[i].split('=')[1] });
      } else values.push({ key: 'url', value: p[i] });
    }
    console.log('values, values)
    

    input query params -> ?url=wwww.xxx.com?id=abc

    result -> [ { key: ‘url’, value: ‘wwww.xxx.com’ }, { key: ‘id’, value: ‘abc’ } ]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search