-1

searchItem Contain some value like abc+def. when i am checking in backend its going like (abc def). how to fixed .+(plus sign convert into space)

search(searchItem: string): Promise<any> {
    let params = new HttpParams();
    params = params.append('searchItem', searchItem);
    return new Promise((resolve, reject) => {
        // use For Testing
        this.httpClient.get(this.urlService.getApiUrl() + 'test/item',
                {
                    params: params
                })
        .subscribe(data => {
            resolve(data);
        }, error => {
            console.log('Error: ' + JSON.stringify(error));
            reject(error);
        });
    });
}
Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
  • 3
    Possible duplicate of [Angular url plus sign converting to space](https://stackoverflow.com/questions/45428842/angular-url-plus-sign-converting-to-space) – TheParam Mar 09 '19 at 04:47

2 Answers2

0

You just need to call encodeURIComponent() method before set param.

...
params = params.append('searchItem', encodeURIComponent(searchItem));
...
zmag
  • 7,825
  • 12
  • 32
  • 42
0

Use a custom encoder for HttpParams to fix this.

import { HttpParameterCodec } from '@angular/common/http';

export 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 then in your search() function,

let params = new HttpParams({encoder: new CustomEncoder()});
Anjil Dhamala
  • 1,544
  • 3
  • 18
  • 37