Git Product home page Git Product logo

angular4-httpclient-wrapper's Introduction

HttpClient Wrapper Integration

Providing HttpClient Service Reference in app.module

Step1: Include HttpClient Module Provided by angular4

=> Import statement for HttpClientModule import { HttpClientModule } from '@angular/common/http';

=> Specify Module name in app.modules[imports].

Example:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {CommonModule} from "@angular/common";
import {FormsModule} from "@angular/forms";
import {HttpWrapperService} from "./HttpWrapper/http-wrapper.service";
import {HttpClientModule} from "@angular/common/http";
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step2: Include HttpClient Wrapper

=> Specify HttpClient Wrapper reference in providers

providers:[HttpWrapperService]

Note:"Please include import statement according to the location of wrapper."

Step3: Include ErrorHandler Service for Http Requests.

=> Specify Error Handler service reference in providers

providers:[HttpWrapperService, HttpErrorHandlerService]

To Include Oauth Interceptor in app.module

import { HTTP_INTERCEPTORS } from '@angular/common/http';
import {AuthInterceptorService} from "./HttpWrapper/auth-interceptor.service";

providers: [{
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptorService,
    multi: true
  }]

Note: can be used to send Oauth Access Token on every API request.

HttpClient Wrapper Usage

To Set Headers

let headers = new HttpHeaders();
headers = headers.append("HeaderName", "HeaderValue");
headers = headers.append("HeaderName", "HeaderValue");

To Set Params

let params = new HttpParams();
params = params.append("ParamName", "ParamValue");
params = params.append("ParamName", "ParamValue");

Get Example

this.httpWrapper.get(URL, OPTIONS);
Example:
this.httpWrapper.get("https://example.com/getData", {headers:headers,params:params});

Post Example

this.httpWrapper.post(URL, OPTIONS);
Example:
this.httpWrapper.post("https://example.com/getData", {headers:headers,params:params, body:body});

Put Example

this.httpWrapper.put(URL, OPTIONS);
Example:
this.httpWrapper.put("https://example.com/getData", {headers:headers,params:params, body:body});

Delete Example

this.httpWrapper.delete(URL, OPTIONS);

Example:

this.httpWrapper.delete("https://example.com/getData", {headers:headers,params:params, body:body});

angular4-httpclient-wrapper's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.