Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Created March 9, 2018 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/d6ec008f880b4e0683f3108d9271d324 to your computer and use it in GitHub Desktop.
Save rahulsahay19/d6ec008f880b4e0683f3108d9271d324 to your computer and use it in GitHub Desktop.
app.component
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import * as _ from 'lodash';
import { Post } from './post';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
/**
*
*/
constructor(private http: HttpClient) {
}
title = 'app';
readonly ROOT_URL= 'https://jsonplaceholder.typicode.com';
posts:Observable<Post[]>;
newPost: Observable<any>;
getPosts(){
let params = new HttpParams().set('userId', '7');
let headers = new HttpHeaders().set('Authorization', 'auth-key');
this.posts =this.http.get<Post[]>(this.ROOT_URL + '/posts', { headers })
}
submitPost(){
const data:Post = {
id:null,
userId :7,
title: 'New Post',
body: 'Hello HTTP Client!'
}
this.newPost = this.http.post<Post>(this.ROOT_URL + '/posts', data)
.map(post => post.title)
}
ngOnInit() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment