Ask coding questions

← Back to all posts
Pass a URL in a querystring?
h
Vandesm14 (1176)

Is it possible for Javascript to send a URL in a querystring/parameter? Like such: https://mysite.com/get?url=https://www.google.com
I have seen that this is possible when looking at the url of https://webcache.googleusercontent.com/search?q=cache:4SjrkOc4UF4J:https://store.steampowered.com/+&cd=1&hl=en&ct=clnk&gl=us

But the question comes about how to do this. I've tried URI encoding, which does not work (the request decodes it and tries to do "https:/store.steam", which is not what I want. Is there any form of conversion that I can do to allow a URL to go into a URL querystring?

PS: I cannot use websockets or anything besides a get request

Answered by TheDrone7 (643) [earned 5 cycles]
View Answer
Commentshotnewtop
TheDrone7 (643)

Yes it is possible, just encode the URL. If you're using JavaScript, there is the built-in encodeUriComponent function. Here's an example: -
let encoded = encodeUriComponent("http://google.co.in"); which will encode the URL to be able to send as a part of the query string in her request. You can later decode it in the same way using the decodeUriComponent function.

Here's how the encoding is done - https://www.w3schools.com/tags/ref_urlencode.asp
You can always use this to build a perfect encoding and decoding function that suits your needs.

Vandesm14 (1176)

@TheDrone7 Yes, but encoding the URL does not work as it is decoded as soon as the get request is sent to my server.

TheDrone7 (643)

@Vandesm14 it might be some other issue then because in my experience that has never happened.