Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Url encode nested query strings

Tags:

url-encoding

I have a the following url:

http://www.test.com/?client=xyz&redirect_uri=http://www.xyz.com/x?p1=one&p2=two

How should I url encode these so that the browser does not interpret p1 and p2 as query strings of the test.com when they are actually part of the redirect_uri parameter.

like image 330
user3156288 Avatar asked Jan 03 '14 07:01

user3156288


1 Answers

I quote this question that uses JavaScript. And this answer by Dustin Boswell.

encodeURIComponent() should work. For example,

'&url=' + encodeURIComponent("http://a.com/?q=query&n=10")

produces

"&url=http%3A%2F%2Fa.com%2F%3Fq%3Dquery%26n%3D10"

(which doesn't have any '&' or '?' in the value). When your server gets this url, it should be able to decode that to get the original:

param["url"] = "http://a.com/?q=query&n=10"

I'm not sure what server you're using (e.g. Rails, Django, ...) but that should work "out of the box" on any normal system.

like image 141
Jean-Paul Avatar answered Oct 12 '22 18:10

Jean-Paul