Plus Sign in Query String

Plus sign in query string

+ sign has a semantic meaning in the query string. It is used to represent a space. Another character that has semantic importance in the query string is & which is used to separate the various var=value pairs in the query string.

Most server side scripts would decode the query parameters before using them, so that a + gets properly converted to a space. Now, if you want a literal + to be present in the query string, you need to specify %2B instead.

+ sign in the query string is URL-decoded to a space. %2B in the query string is URL-decoded to a + sign.

See the difference between

http://www.google.com/search?q=foo+bar

and

http://www.google.com/search?q=foo%2Bbar

In the above examples, Google's server script is URL-decoding the query parameters and then using them to do the search.

URL-encoding is nothing but % sign followed by the hex-code of the special character. For example, we know that the hex code of A is 0x41 (decimal: 65). Try this:

http://www.google.com/search?q=%41

Hope this makes URL-encoding clear.

So, if you want the + sign to be preserved when a JavaScript is fetching a URL with + signs in its query parameters and a server side script would process the query parameters after URL-decoding it, you should URL-encode the query parameters in the URL before using issuing the HTTP get request so that all + signs are converted to %2B's when the request reaches the server side script. Now when the server side script URL-decodes the query string, all %2B's gets converted back to + signs which is what you want.

See Encode URL in JavaScript? to learn how to URL-encode the parameters using JavaScript. Short answer from the discussion there:

var encodedURL = "http://example.com/foo.php?var=" + encodeURIComponent(param);

Plus sign in query string?

The answer you link to just mentions using Server.UrlEncode, not Server.UrlDecode. When you read from Request.Querystring it automatically decodes the string for you. Doing it manually a second time is corrupting it and is why you're getting a space.

Remove plus sign (+) in URL query string

If that's what you are doing, the plus sign will not be the only one that is going to give you a hard time. The apostrophe ('), equals (=), plus (+) and basically anything not in the permitted URL characters (see Percent-encoding @ Wikipedia) is going to get escaped.

You are most likely looking for the decodeURIComponent function.

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

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"] = "someemail@somedomain.com";
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

Allow plus + sign in URL

The proper way to encode a + sign in a URL query string parameter is %2B. ASP.NET will automatically decode this as + for you.

If your users use a proper URL encoding library, this encoding will happen automatically:

string equation = "1 + 2 - 3";
Console.WriteLine(HttpUtility.UrlEncode(equation));

Output:

1+%2b+2+-+3

SpringBoot urlencodes plus sign in query parameters in an inconsistent manner

See this issue

The key to understand this, is that different degrees of encoding are
applied to the URI template vs URI variables. In other words given:

http://example.com/a/{b}/c?q={q}&p={p}

The URI template is everything except for the URI variable
placeholders. However the code snippet you showed only builds a URI
literal without any variables, so the level encoding is the same, only
illegal characters, no matter which method is used.

So it would also have to be something like:

.queryParam("foo", "{foo}").buildAndExpand(foo)

Therefore the idea is to use something like:

builder
.path("/param")
.queryParam("p", "{value}")
.build(value)

to get the query param encoded.

Angular url plus sign converting to space

I have found solution and posting it for future reference. Angular js was converting + sign into %2B.

Following code prevented that:

.config([
'$provide', function($provide) {
$provide.decorator('$browser', function($delegate) {
let superUrl = $delegate.url;
$delegate.url = (url, replace) => {
if(url !== undefined) {
return superUrl(url.replace(/\%2B/g,"+"), replace);
} else {
return superUrl().replace(/\+/g,"%2B");
}
};
return $delegate;
});
}
])

Plus sign in query string for ASP.NET site

Well, of course you can't put a space in a URI in any case. What you can do is put %20 in which is the way to encode a space at any point in a URI as well as + being the normal way to encode a space in the query portion, and the recommended way to do so in that portion of it because %20 can cause problems.

Because of this is an implementaiton detail application/x-www-form-urlencoded and we normally care about the actual data sent, Request.QueryString[] does the unescaping for you, turning both + and %20 into a space.

You want to look at the Request.RawUrl (returns a string) or Request.Url which returns a Uri. Probably the easiest is to feed Request.Url into a UriBuilder so you can change just the query and get a Uri back.

Still, I'd recommend the opposite approach, if you're having issues with duplicate content due to the two possible ways of encoding a space in the query string, go with the recommended norm and turn the cases of %20 into + rather than the other way around.

var u = Request.Url;
if(u.Query.Contains("%20"))
{
var ub = new UriBuilder(u);
Console.WriteLine(ub.Query);
string query = ub.Query;
//note bug in Query property - it includes ? in get and expects it not to be there on set
ub.Query = ub.Query.Replace("%20", "+").Substring(1);
Response.StatusCode = 301;
Response.RedirectLocation = ub.Uri.AbsoluteUri;
Response.End();
}

+' (plus sign) not encoded with RestTemplate using String url, but interpreted as ' ' (space)

We realized the URL can be modified in an interceptor after the encoding is done. So a solution would be to use an interceptor, that encodes the plus sign in the query params.

RestTemplate restTemplate = new RestTemplateBuilder()
.rootUri("http://localhost:8080")
.interceptors(new PlusEncoderInterceptor())
.build();

A shortened example:

public class PlusEncoderInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return execution.execute(new HttpRequestWrapper(request) {
@Override
public URI getURI() {
URI u = super.getURI();
String strictlyEscapedQuery = StringUtils.replace(u.getRawQuery(), "+", "%2B");
return UriComponentsBuilder.fromUri(u)
.replaceQuery(strictlyEscapedQuery)
.build(true).toUri();
}
}, body);
}
}


Related Topics



Leave a reply



Submit