Angular 6 : API Get and Post Method
Get
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { ProductSearch } from '../Model/CountrySearch';
import { TokenParams } from '../Model/TokenParams';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs';
@Injectable()
export class CountrySearchService {
user = new TokenParams();
constructor(private http: Http) {
this.user = JSON.parse(localStorage.getItem('loggedInUser'));
}
getCountryDetail() : Observable<CountrySearch[]>
{
var headersAPI = new Headers();
headersAPI.append('Authorization' , 'Bearer ' + this.user.access_token);
return this.http.get(environment.apiUrl + environment.countrysearch,
{headers : headersAPI}).map(res => res.json());
}
}
POST
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { ProductSearch } from '../Model/CountrySearch';
import { TokenParams } from '../Model/TokenParams';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs';
@Injectable()
export class CountrySearchService {
user = new TokenParams();
constructor(private http: Http) {
this.user = JSON.parse(localStorage.getItem('loggedInUser'));
}
getCountryDetail22(searchText: string, pageNumber : number ) {
var httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8',
'Authorization' : 'Bearer ' + this.user.access_token
}),
};
return this.http.post(environment.apiUrl + environment.countrysearch,
{
PageSize:10,
PageNumber:pageNumber,
Search: searchText
},
this.httpOptions);
}
}
Hope this will help you and save your time.
Enjoy !!!
:)