Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active March 12, 2023 14:50
Show Gist options
  • Save dherges/442d3a7bc65e3e46d8dead728e4d8be8 to your computer and use it in GitHub Desktop.
Save dherges/442d3a7bc65e3e46d8dead728e4d8be8 to your computer and use it in GitHub Desktop.
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));
}
}
@vaibhavTecorb
Copy link

Helped a lot. Thanks.

@chandrashekhar-developer

thanks you saved my time..

@nikensss
Copy link

This helped a lot. Thank you very much!

@Vasiakozak1
Copy link

Thanks!

@Rancha124
Copy link

Rancha124 commented Jan 21, 2021

This is not working in Angular 4, anyone have an idea regarding that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment