How to encode the plus (+) symbol in a URL

The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) signs in the email body with blank space. It looks like it only happens with the + sign. How can I remedy this? (I am working on a ASP.NET web page.)

https://mail.google.com/mail?view=cm&tf=0&[email protected]&su=some subject&body=Hi there+Hello there

(In the body email, “Hi there+Hello there” will show up as “Hi there Hello there”)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The + character has a special meaning in a URL => it means whitespace – . If you want to use the literal + sign, you need to URL encode it to %2b:

body=Hi+there%2bHello+there

Here’s an example of how you could properly generate URLs in .NET:

var uriBuilder = new UriBuilder("https://mail.google.com/mail");

var values = HttpUtility.ParseQueryString(string.Empty);
values["view"] = "cm";
values["tf"] = "0";
values["to"] = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b1adafa7a7afa3abae82b1adafa7a6adafa3abaceca1adaf">[email protected]</a>";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";

uriBuilder.Query = values.ToString();

Console.WriteLine(uriBuilder.ToString());

The result

https://mail.google.com:443/mail?view=cm&tf=0&to=someemail%40somedomain.com&su=some+subject&body=Hi+there%2bHello+there

Method 2

If you want a plus + symbol in the body you have to encode it as 2B.

For example:
Try this

Method 3

In order to encode + value using JavaScript, you can use encodeURIComponent function.

Example:

var url = "+11";
var encoded_url = encodeURIComponent(url);
console.log(encoded_url)

Method 4

It’s safer to always percent-encode all characters except those defined as “unreserved” in RFC-3986.

unreserved = ALPHA / DIGIT / “-” / “.” / “_” / “~”

So, percent-encode the plus character and other special characters.

The problem that you are having with pluses is because, according to RFC-1866 (HTML 2.0 specification), paragraph 8.2.1. subparagraph 1., “The form field names and values are escaped: space characters are replaced by `+’, and then reserved characters are escaped”). This way of encoding form data is also given in later HTML specifications, look for relevant paragraphs about application/x-www-form-urlencoded.

Method 5

Just to add this to the list:

Uri.EscapeUriString("Hi there+Hello there") // Hi%20there+Hello%20there
Uri.EscapeDataString("Hi there+Hello there") // Hi%20there%2BHello%20there

See https://stackoverflow.com/a/34189188/98491

Usually you want to use EscapeDataString which does it right.

Method 6

Generally if you use .NET API’s – new Uri("someproto:with+plus").LocalPath or AbsolutePath will keep plus character in URL. (Same "someproto:with+plus" string)

but Uri.EscapeDataString("with+plus") will escape plus character and will produce "with%2Bplus".

Just to be consistent I would recommend to always escape plus character to "%2B" and use it everywhere – then no need to guess who thinks and what about your plus character.

I’m not sure why from escaped character '+' decoding would produce space character ' ' – but apparently it’s the issue with some of components.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x