I have angular application where i want to pass plus sign + in query string like:
http://localhost:3000/page?name=xyz+manwal
When I am hitting this URL its converting to:
http://localhost:3000/page?name=xyz%20manwal
Where %20 refer to space . How can I prevent this conversion?
A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.
Spaces are not allowed in URLs. They should be replaced by the string %20. In the query string part of the URL, %20 can be abbreviated using a plus sign (+).
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces.
So, only after "?", spaces can be replaced by pluses. In other cases, spaces should be encoded to %20. But since it's hard to determine the context correctly, it's the best practice to never encode spaces as "+".
You can override default angular encoding with adding Interceptor which fixes this:
import { HttpInterceptor, HttpRequest, HttpEvent, HttpHandler, HttpParams, HttpParameterCodec } from "@angular/common/http"; import { Injectable } from "@angular/core"; import { Observable } from "rxjs"; @Injectable() export class EncodeHttpParamsInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const params = new HttpParams({encoder: new CustomEncoder(), fromString: req.params.toString()}); return next.handle(req.clone({params})); } } class CustomEncoder implements HttpParameterCodec { encodeKey(key: string): string { return encodeURIComponent(key); } encodeValue(value: string): string { return encodeURIComponent(value); } decodeKey(key: string): string { return decodeURIComponent(key); } decodeValue(value: string): string { return decodeURIComponent(value); } }
and declare it in providers section of in app.module.ts
providers: [ { provide: HTTP_INTERCEPTORS, useClass: EncodeHttpParamsInterceptor, multi: true } ]
This ia a common problem. The +
character is used by the URL to separate two words. In order to use the +
character in the parameter values, you need to encode your parameter values before adding them as part of the URL. Javascript / TypeScript provide a encodeURI()
function for that specific purpose.
URL encoding converts characters into a format that can be transmitted over the Internet. [w3Schools Reference]
Here is how you can fix this problem:
let encodedName = encodeURI('xyz+manwal'); let encodedURI = 'http://localhost:3000/page?name='+encodedName; //.. OR using string interpolation let encodedURI = `http://localhost:3000/page?name=${ encodedName }`;
In the same way, you can decode the parameters using decodeURI()
method.
let decodedValue = decodeURI(encodedValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With