1

I have the following express js route which helps in redirecting to the given URL.

app.get("/", async (req, res) => {
  const url = req.query.url;
  res.redirect(url);
});

It's working fine. But the problem is, somehow the passed query strings is getting cut off for no reason.

Example:

This is what I'm actually passing.

http://localhost:3000/?url=https://google.com/?abc=123&def=456&aaa=098

After redirecting whatever after the first query string goes missing.

It's appearing like this.

https://google.com/?abc=123

Not sure why &def=456&aaa=098 goes missing.

1 Answer 1

1

Use the encodeURIComponent and decodeURIComponent functions.

For example:

app.get("/", async (req, res) => {
  const url = req.query.url;
  res.redirect(decodeURIComponent(url));
});

Note that the url parameter must be encoded in the actual URL.
Your URL should look something like this:

http://localhost:3000/?url=https://google.com/%3Fabc%3D123%26def%3D456%26aaa%3D098
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.