Lucas Yver
Lucas Yver

Reputation: 11

HttpParams issue Angular 4, bad params

I don't understant why, and I don't find the answer anywhere.

I have this issue when i want use params in http request.

identificationClientMAil(phone: string, mail: string) : Observable <any>{
    let params = new HttpParams().append('phone', phone).append('mail', mail);

    return this.http.get('http://localhost:8000/Programme/extranet-api/public', { headers: this.headers, params: params })
    .map(res => res.json())
    .catch(err => {
       throw 'error in source. Details: ' + err;
     });
  }

And my url look like this after:

http://localhost:8000/Programme/extranet-api/public?updates=%7B%22param%22:%22phone%22,%22value%22:%22ddddddd%22,%22op%22:%22a%22%7D&updates=%7B%22param%22:%22mail%22,%22value%22:%22%22,%22op%22:%22a%22%7D&cloneFrom=%7B%22updates%22:null,%22cloneFrom%22:null,%22encoder%22:%7B%7D,%22map%22:null%7D&encoder=%7B%7D&map=null

Where is my problem ?

Upvotes: 1

Views: 194

Answers (2)

What kind of issue do you have? It can also be a server issue (incorrect path to resource or other). Anyway Dhyey had a good suggestion using URLSearchParams, though it is a better idea to let http.get handle the params conversion (as you had in your code) like:

return this.http.get('http://localhost:8000/Programme/extranet-api/public', 
      { headers: this.headers, search: myUrlSearchParams })...;

Upvotes: 1

Dhyey
Dhyey

Reputation: 4335

You need to use URLSearchParams:

let params = new URLSearchParams();
urlSearchParams.append('phone', phone);
urlSearchParams.append('mail', mail);
return this.http.get('http://localhost:8000/Programme/extranet-api/public?' + urlSearchParams.toString(), { headers: this.headers })
.map(res => res.json());
.catch(err => {
   throw 'error in source. Details: ' + err;
 });

Upvotes: 1

Related Questions