Angular 2 Http
@NgModule({
declarations: [],
imports: [HttpModule],
bootstrap: [],
providers: []
})
constructor(private http: Http) {}
|
GET
|
get(url: string, options?: RequestOptionsArgs) : Observable<Response> Performs a request with get http method.
|
|
POST
|
post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> Performs a request with post http method.
|
|
PUT
|
put(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> Performs a request with put http method.
|
|
DELETE
|
delete(url: string, options?: RequestOptionsArgs) : Observable<Response> Performs a request with delete http method.
|
|
PATCH
|
patch(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> Performs a request with patch http method.
|
|
HEAD
|
head(url: string, options?: RequestOptionsArgs) : Observable<Response> Performs a request with head http method.
|
|
OPTIONS
|
options(url: string, options?: RequestOptionsArgs) : Observable<Response> Performs a request with options http method.
|
interface RequestOptionsArgs {
url : string
method : string|RequestMethod
search : string|URLSearchParams
headers : Headers
body : any
withCredentials : boolean
responseType : ResponseContentType
}
class Headers {
staticfromResponseHeaderString(headersString: string) : Headers
constructor(headers?: Headers|{[name: string]: any})
append(name: string, value: string) : void
delete(name: string) : void
forEach(fn: (values: string[], name: string, headers: Map<string, string[]>) => void) : void
get(name: string) : string
has(name: string) : boolean
keys() : string[]
set(name: string, value: string|string[]) : void
values() : string[][]
toJSON() : {[name: string]: any}
getAll(name: string) : string[]
entries()
}
import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from "@angular/http";
import 'rxjs/Rx';
import {Observable} from "rxjs";
import {Post} from "./http/post.class";
@Injectable()
export class HttpService {
constructor(private http: Http) { }
private requestUrl: string = 'http://localhost:4000/posts';
//Do all methods and observable options
getData(id : number) : Observable<Post> {
return this.http.get(`${this.requestUrl}/${id}`)
.map(this.mapResponse)
.catch(this.handleError)
}
handleError(error: any): Observable<any> {
console.error('An error occurred', error);
return Observable.throw(error.json() || 'Server error');
}
mapResponse(response : Response) : Post {
return response.json();
}
addData(body : Post) : Observable<Post> {
let bodyString = JSON.stringify(body);
let header = new Headers({
'Content-Type' : 'application/json'
});
let options = new RequestOptions({
headers : header
});
return this.http.post(`${this.requestUrl}`, bodyString, options)
.map(this.mapResponse)
.catch(this.handleError);
}
updateData(body : Post) : Observable<Post> {
let bodyString = JSON.stringify(body);
let header = new Headers({
'Content-Type' : 'application/json'
});
let options = new RequestOptions({
headers : header
});
return this.http.put(`${this.requestUrl}/${body.id}`, bodyString, options)
.map(this.mapResponse)
.catch(this.handleError);
}
deleteData(body : Post) : Observable<Post> {
return this.http.delete(`${this.requestUrl}/${body.id}`)
.map(this.mapResponse)
.catch(this.handleError);
}
}
getData(id : number) : Observable<Post> {
return this.http.get(`${this.requestUrl}/${id}`)
.map(this.mapResponse)
.catch(this.handleError)
}
mapResponse(response : Response) : Post {
return response.json();
}
getData(id : number) : Observable<Post> {
return this.http.get(`${this.requestUrl}/${id}`)
.map(this.mapResponse)
.catch(this.handleError)
}
handleError(error: any): Observable<any> {
console.error('An error occurred', error);
return Observable.throw(error.json() || 'Server error');
}
|
observerOrNext
|
PartialObserver<T> | ((value: T) => void)
|
|
error
|
(error: any) => void
|
|
complete
|
() => void
|
private requestUrl: string = 'http://localhost:4000/posts';
getData(id : number) : Observable<Post> {
return this.http.get(`${this.requestUrl}/${id}`)
.map(this.mapResponse)
.catch(this.handleError)
}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.