Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A close look to Angular's new HttpClient

A close look to Angular's new HttpClient

Presentation from ng-be in December 2017.

Manfred Steyer

December 09, 2017
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. About me … • Manfred Steyer • Angular Workshops and

    Consultancy • SOFTWAREarchitekt.at • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
  2. Http in Angular Http (Service) • Angular 2+ • Most

    Common Use Cases (80:20 principle) • Experimental • Deprecated with 5.0 HttpClient • Angular 4.3 • A lot of additional features • Full comfort we know from $http
  3. Contents • Short overview of Basics • Custom Data Formats

    (Binary, Text) • Progress Information • Interceptors for Extensions • Token-based Security
  4. HttpClient HttpClientModule and HttpClient from @angular/common/http let url = 'http://www.angular.at/api/hotel';

    let headers = new HttpHeaders() .set('Accept', 'application/json'); let params = new HttpParams() .set('city', this.city); this .http .get<Hotel[]>(url, { headers, params }) .subscribe( hotels => console.debug('hotels', hotels), err => console.error('err', err) );
  5. Getting the full HttpResponse save(entity: HotelBooking): Observable<string> { let url

    = […] let headers = […] return this.http .post<HotelBooking>(url, entity, {headers, observe: 'response'}) […] }
  6. Getting the full HttpResponse save(entity: HotelBooking): Observable<string> { let url

    = […] let headers = […] return this.http .post<HotelBooking>(url, entity, {headers, observe: 'response'}) .pipe( map((response: HttpResponse<HotelBooking>) => { console.debug('status', response.status); console.debug('body', response.body); return response.headers.get('Location'); })); }
  7. Testing it('can load hotels by city', () => { let

    service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); […] });
  8. Testing it('can load hotels by city', () => { let

    service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); […] });
  9. Testing it('can load hotels by city', () => { let

    service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); let req = ctrl.expectOne('/hotel?city=Graz'); expect(req.request.method).toBe('GET'); […] });
  10. Testing it('can load hotels by city', () => { let

    service: HotelService = TestBed.get(HotelService); let ctrl: HttpTestingController = TestBed.get(HttpTestingController); service.find({ city: 'Graz' }).subscribe( hotels => expect(hotels.length).toBe(2), err => fail(err) ); let req = ctrl.expectOne('/hotel?city=Graz'); expect(req.request.method).toBe('GET'); req.flush([{id: 1, name: 'Hotel Mama'}, {id: 2, name: 'Budget Hotel'}]); ctrl.verify(); });
  11. Using XML findById(id: string): Observable<HotelBooking> { […] return this.http .get(url,

    { headers, params, responseType: 'text'}) .pipe( switchMap(xmlString => { let observableFactory = bindCallback(parseString); return observableFactory(xmlString, parserOptions); }), map(js => js[1].hotelBooking) ); } import { parseString } from 'xml2js';
  12. Idea • Providing a hook • Modify requests and responses

    globally • Including Headers, e. g. for Auth. • Parsing Data Formats, like XML • Error Handling • Caching • …