Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular url plus sign converting to space

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?

like image 836
Manwal Avatar asked Aug 01 '17 04:08

Manwal


People also ask

Why is space %20 in URL?

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.

How do you handle space in URL?

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 (+).

Can URL params have spaces?

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.

How do I change a space in URL?

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 "+".


2 Answers

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     } ] 
like image 181
Piotr Korlaga Avatar answered Oct 01 '22 02:10

Piotr Korlaga


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); 
like image 39
Faisal Avatar answered Oct 01 '22 03:10

Faisal