Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Angular HttpClient (5)
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
/** This class implements some features that should be tested. */
@Injectable()
export class HttpClientFeatureService {
constructor(
private http: HttpClient
) {}
login(user: string, password: string): Observable<boolean> {
const body = new HttpParams()
.set(`user`, user)
.set(`password`, password);
const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
return this.http.post(`auth/login`, body.toString(), { headers, observe: 'response' })
.map((res: HttpResponse<Object>) => res.ok)
.catch((err: any) => Observable.of(false));
}
}
@kamok

This comment has been minimized.

Copy link

@kamok kamok commented Jan 10, 2018

This is probably the only way I was able to successfully do a application/x-www-form-urlencoded request with Angular 5. I spent 1 hour looking for a solution.

@muthu0101

This comment has been minimized.

Copy link

@muthu0101 muthu0101 commented Feb 6, 2018

Great Job, it's really helped me a lot.

@fsmaiorano

This comment has been minimized.

Copy link

@fsmaiorano fsmaiorano commented Feb 12, 2018

Great Job! I was with the same problem!

@marija126

This comment has been minimized.

Copy link

@marija126 marija126 commented Mar 20, 2018

This worked for me too! Thank you!

@cristhoz

This comment has been minimized.

Copy link

@cristhoz cristhoz commented Apr 15, 2018

Thank you!

@JhonBv

This comment has been minimized.

Copy link

@JhonBv JhonBv commented Jul 4, 2018

Really well done mate...!

@yeisson

This comment has been minimized.

Copy link

@yeisson yeisson commented Jul 4, 2018

Works with Angular 6 too, thanks

@JhonBv

This comment has been minimized.

Copy link

@JhonBv JhonBv commented Jul 5, 2018

There is an easier way though!

let body = new URLSearchParams();
body.set('username', this.username);
body.set('password', this.password);
body.set('grant_type', gtype);
body.set('client_id', clid);
body.set('client_secret', secid);
URLSearchParams() will automatically encode all of the body content in application/x-www-form-urlencoded

@dherges

This comment has been minimized.

Copy link
Owner Author

@dherges dherges commented Oct 24, 2018

HttpParams implements encoding: https://github.com/angular/angular/blob/7.0.0/packages/common/http/src/params.ts#L179-L185

URLSearchParams and HttpParams looks like the same.

@Nexeuz

This comment has been minimized.

Copy link

@Nexeuz Nexeuz commented Oct 24, 2018

Thanks!

@vaibhavTecorb

This comment has been minimized.

Copy link

@vaibhavTecorb vaibhavTecorb commented May 11, 2019

Helped a lot. Thanks.

@chandrashekhar-developer

This comment has been minimized.

Copy link

@chandrashekhar-developer chandrashekhar-developer commented Mar 13, 2020

thanks you saved my time..

@nikensss

This comment has been minimized.

Copy link

@nikensss nikensss commented Mar 14, 2020

This helped a lot. Thank you very much!

@Vasiakozak1

This comment has been minimized.

Copy link

@Vasiakozak1 Vasiakozak1 commented Apr 27, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.