Http post and get request in angular 6

后端 未结 3 1679
时光取名叫无心
时光取名叫无心 2020-12-14 00:51

In angular 5.2.x for http get and post I had this code:

post(url: string, model: any): Observable {

return this.http.post(url, model)
  .map(         


        
3条回答
  •  温柔的废话
    2020-12-14 01:37

    You can do a post/get using a library which allows you to use HttpClient with strongly-typed callbacks.

    The data and the error are available directly via these callbacks.

    The library is called angular-extended-http-client.

    angular-extended-http-client library on GitHub

    angular-extended-http-client library on NPM

    Very easy to use.

    Traditional approach

    In the traditional approach you return Observable<HttpResponse<T>> from Service API. This is tied to HttpResponse.

    With this approach you have to use .subscribe(x => ...) in the rest of your code.

    This creates a tight coupling between the http layer and the rest of your code.

    Strongly-typed callback approach

    You only deal with your Models in these strongly-typed callbacks.

    Hence, The rest of your code only knows about your Models.

    Sample usage

    The strongly-typed callbacks are

    Success:

    • IObservable<T>
    • IObservableHttpResponse
    • IObservableHttpCustomResponse<T>

    Failure:

    • IObservableError<TError>
    • IObservableHttpError
    • IObservableHttpCustomError<TError>

    Add package to your project and in your app module

    import { HttpClientExtModule } from 'angular-extended-http-client';
    

    and in the @NgModule imports

      imports: [
        .
        .
        .
        HttpClientExtModule
      ],
    

    Your Models

    
    export class SearchModel {
        code: string;
    }
    
    //Normal response returned by the API.
    export class RacingResponse {
        result: RacingItem[];
    }
    
    //Custom exception thrown by the API.
    export class APIException {
        className: string;
    }
    

    Your Service

    In your Service, you just create params with these callback types.

    Then, pass them on to the HttpClientExt's get method.

    import { Injectable, Inject } from '@angular/core'
    import { SearchModel, RacingResponse, APIException } from '../models/models'
    import { HttpClientExt, IObservable, IObservableError, ResponseType, ErrorType } from 'angular-extended-http-client';
    .
    .
    
    @Injectable()
    export class RacingService {
    
        //Inject HttpClientExt component.
        constructor(private client: HttpClientExt, @Inject(APP_CONFIG) private config: AppConfig) {
    
        }
    
        //Declare params of type IObservable and IObservableError.
        //These are the success and failure callbacks.
        //The success callback will return the response objects returned by the underlying HttpClient call.
        //The failure callback will return the error objects returned by the underlying HttpClient call.
        searchRaceInfo(model: SearchModel, success: IObservable, failure?: IObservableError) {
            let url = this.config.apiEndpoint;
    
            this.client.post(url, model, 
                                                          ResponseType.IObservable, success, 
                                                          ErrorType.IObservableError, failure);
        }
    }
    

    Your Component

    In your Component, your Service is injected and the searchRaceInfo API called as shown below.

      search() {    
    
    
        this.service.searchRaceInfo(this.searchModel, response => this.result = response.result,
                                                      error => this.errorMsg = error.className);
    
      }
    

    Both, response and error returned in the callbacks are strongly typed. Eg. response is type RacingResponse and error is APIException.

提交回复
热议问题