Escaping ampersand in URL

HttpUrlGetQuery StringQuery Parameters

Http Problem Overview


I am trying to send a GET message that contains strings with ampersands and can't figure how to escape the ampersand in the URL.

Example:

http://www.example.com?candy_name=M&M
result => candy_name = M

I also tried:

http://www.example.com?candy_name=M\&M
result => candy_name = M\\

I am using URLs manually, so I just need the correct characters.

I can't use any libraries. How can it be done?

Http Solutions


Solution 1 - Http

They need to be percent-encoded:

> encodeURIComponent('&')
"%26"

So in your case, the URL would look like:

http://www.mysite.com?candy_name=M%26M

Solution 2 - Http

This does not only apply to the ampersand in URLs, but to all reserved characters. Some of which include:

 # $ & + ,  / : ; = ? @ [ ]

The idea is the same as encoding an & in an HTML document, but the context has changed to be within the URI, in addition to being within the HTML document. So, the percent-encoding prevents issues with parsing inside of both contexts.

The place where this comes in handy a lot is when you need to put a URL inside of another URL. For example, if you want to post a status on Twitter:

http://www.twitter.com/intent/tweet?status=What%27s%20up%2C%20StackOverflow%3F(http%3A%2F%2Fwww.stackoverflow.com)

There's lots of reserved characters in my Tweet, namely ?'():/, so I encoded the whole value of the status URL parameter. This also is helpful when using mailto: links that have a message body or subject, because you need to encode the body and subject parameters to keep line breaks, ampersands, etc. intact.

> When a character from the reserved set (a "reserved character") has > special meaning (a "reserved purpose") in a certain context, and a URI > scheme says that it is necessary to use that character for some other > purpose, then the character must be percent-encoded. Percent-encoding > a reserved character involves converting the character to its > corresponding byte value in ASCII and then representing that value as > a pair of hexadecimal digits. The digits, preceded by a percent sign > ("%") which is used as an escape character, are then used in the URI > in place of the reserved character. (For a non-ASCII character, it is > typically converted to its byte sequence in UTF-8, and then each byte > value is represented as above.) The reserved character "/", for > example, if used in the "path" component of a URI, has the special > meaning of being a delimiter between path segments. If, according to a > given URI scheme, "/" needs to be in a path segment, then the three > characters "%2F" or "%2f" must be used in the segment instead of a raw > "/".

http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters

Solution 3 - Http

Try using http://www.example.org?candy_name=M%26M.

See also this reference and some more information on Wikipedia.

Solution 4 - Http

You can use the % character to 'escape' characters that aren't allowed in URLs. See RFC 1738.

A table of ASCII values is given on the Wikipedia page.

You can see & is 26 in hexadecimal - so you need M%26M.

Solution 5 - Http

I would like to add a minor comment to Blender's solution.

You can do the following:

var link = 'http://example.com?candy_name=' + encodeURIComponent('M&M');

That outputs:

http://example.com?candy_name=M%26M

The great thing about this it does not only work for &, but for any especial character.

For instance:

var link = 'http://example.com?candy_name=' + encodeURIComponent('M&M?><')

Outputs:

"http://example.com?candy_name=M%26M%3F%3E%3C"

Solution 6 - Http

This may help if someone want it in PHP

$variable ="candy_name=M&M";
$variable = str_replace("&", "%26", $variable);

Solution 7 - Http

If you can't use any libraries to encode the value, http://www.urlencoder.org/ or http://www.urlencode-urldecode.com/ or ...

Just enter your value "M&M", not the full URL ;-)

Solution 8 - Http

You can rather pass your arguments using this encodeURIComponent function so you don't have to worry about passing any special characters.

data: "param1=getAccNos&param2="+encodeURIComponent('Dolce & Gabbana') OR
var someValue = 'Dolce & Gabbana';
data : "param1=getAccNos&param2="+encodeURIComponent(someValue)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontomermesView Question on Stackoverflow
Solution 1 - HttpBlenderView Answer on Stackoverflow
Solution 2 - HttpAlex WView Answer on Stackoverflow
Solution 3 - HttpgkalpakView Answer on Stackoverflow
Solution 4 - HttpPeter HullView Answer on Stackoverflow
Solution 5 - HttpAntonioView Answer on Stackoverflow
Solution 6 - HttpMohamed RamadanView Answer on Stackoverflow
Solution 7 - HttpOliverView Answer on Stackoverflow
Solution 8 - HttpdeilkalbView Answer on Stackoverflow