169

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")

4
  • 1
    This is a top search engine hit for "site:stackoverflow.com plus URL encode link". This is acceptable, though there must be an older canonical question somewhere, after nearly 3 years in. For instance, one that is language and framework-agnostic (in case that is the real question, not how it specifically achieved). Nov 27, 2022 at 21:30
  • A corresponding one for Python (framework-agnostic). Nov 27, 2022 at 21:57
  • A corresponding one for JavaScript (framework-agnostic). Nov 27, 2022 at 22:11
  • What is the language and framework-agnostic Stack Overflow question? Nov 27, 2022 at 22:26

7 Answers 7

246

The + character has a special meaning in [the query segment of] a URL => it means whitespace: . If you want to use the literal + sign there, 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"] = "[email protected]";
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

9
  • 5
    The RFC clearly says that the + sign can be used unencoded, and also if it had to be encoded, there is no reason to turn it into a "space" character. Maybe you can point us to a proper document of a standard mentioning what is that of translating a + symbol into a space symbol and vice-versa. Nov 22, 2013 at 18:44
  • 4
    Yeah what are you talking about? I've never seen an RFC or anything that says + means spaces....
    – freedrull
    Dec 14, 2015 at 7:56
  • 2
    Warning: If you use the Uri property of UriBuilder you will get a bad result. uriBuilder.Uri.ToString() in your example would return Hi+there+Hello+there. Using uriBuilder.Uri.AbsoluteUri seems to work correctly and stackoverflow.com/a/15120429/1931573 suggests this is fixed in .NET 4.5. As far as RFC goes, the HTML 4 spec says the URL querystring is of type application/x-www-form-urlencoded which itself defines (+) as meaning space. So it's not an RFC but is part of the HTML standard.
    – Nick
    Jun 14, 2016 at 23:22
  • 2
    Note that IIS considers this "double encoding" and will often block a url loaded this way with this error: HTTP Error 404.11 - Not Found The request filtering module is configured to deny a request that contains a double escape sequence. Nov 9, 2018 at 20:51
  • 9
    @PabloAriel According to a W3C document addressing URLs: "Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces."
    – Dave F
    May 28, 2021 at 3:03
36

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

For example: Try this

2
  • 2
    Literally as %2B? Jan 3, 2021 at 4:34
  • Yes, %2B is what you want!
    – mdev
    Apr 20, 2022 at 20:29
13

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

Example:

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

1
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.

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.

3

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.

1
  • Thanks, it works for me on QRCode perfectly
    – Monzur
    Jul 6, 2022 at 18:19
0

Use encodeURLComponent

const email = "[email protected]"
const encodedEmail = encodeURIComponent(email)
const newurl = `https://something.com/test?email=${encodedEmail}`

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.