{"id":317,"date":"2018-04-24T07:01:50","date_gmt":"2018-04-24T01:16:50","guid":{"rendered":"http:\/\/sulavpaudel.com.np\/?p=317"},"modified":"2018-04-24T07:09:27","modified_gmt":"2018-04-24T01:24:27","slug":"angular-4-3-httpclient-accessing-rest-web-services-with-angular","status":"publish","type":"post","link":"https:\/\/sulavpaudel.com.np\/blog\/angular-4-3-httpclient-accessing-rest-web-services-with-angular\/","title":{"rendered":"Angular 4.3 HttpClient (Accessing REST Web Services With Angular)"},"content":{"rendered":"
In Angular 4.3 the new\u00a0HttpClientModule<\/em><\/strong><\/span>\u00a0has been introduced. This new module is available in package\u00a0@angular\/common\/http<\/em><\/span><\/strong>\u00a0and a complete re-implementation of the former\u00a0HttpModule<\/em><\/strong><\/span>. The new\u00a0HttpClient<\/em><\/span><\/strong>\u00a0service is included in\u00a0HttpClientModule<\/em><\/strong><\/span>\u00a0and can be used to initiate HTTP request and process responses within your application.<\/p>\n Let\u2019s see how to use the new\u00a0HttpClient<\/em>\u00a0in your Angular 4.3 project.<\/p>\n To be able to use the\u00a0HttpClient<\/em>\u00a0service within your components we first need to include the\u00a0HttpClientModule<\/em>\u00a0in the Angular application. First we need to import\u00a0HttpClient<\/em>\u00a0module in the application\u2019s root module in file\u00a0app.module.ts<\/em>:<\/p>\n <\/p>\n <code><\/p>\n <\/code><\/p>\n Once imported you can make use of\u00a0HttpClient<\/em>\u00a0in your components. To make\u00a0HttpClient<\/em>\u00a0available in the component class you need to inject it into the class constructor like you can see in the following:<\/p>\n <\/p>\n <code><\/p>\n <\/code><\/p>\n HttpClient<\/em>\u00a0will use the\u00a0XMLHttpRequest<\/em>\u00a0browser API to execute HTTP request. In order to execute HTTP request of a specific type you can use the following methods which corresponds to HTTP verbs:<\/p>\n Let\u2019s implement a simple example which uses GitHub\u2019s REST API to request user data. Insert the following code in file\u00a0app.component.ts<\/em>:<\/p>\n <code><\/p>\n <\/code><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In Angular 4.3 the new\u00a0HttpClientModule\u00a0has been introduced. This new module is available in package\u00a0@angular\/common\/http\u00a0and a complete re-implementation of the former\u00a0HttpModule. The new\u00a0HttpClient\u00a0service is included in\u00a0HttpClientModule\u00a0and can be used to initiate HTTP request and process responses within your application. Let\u2019s see how to use the new\u00a0HttpClient\u00a0in your Angular 4.3 project. Making HttpClient Available In The\u00a0Project To […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[97],"tags":[101,98,102,100],"courses":[],"units":[],"jetpack_publicize_connections":[],"acf":[],"yoast_head":"\nMaking HttpClient Available In The\u00a0Project<\/h3>\n
import { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule } from '@angular\/core';\r\nimport { HttpClientModule } from '@angular\/common\/http';\r\nimport { AppComponent } from '.\/app.component';<\/pre>\n
@NgModule({\r\n declarations: [\r\n AppComponent\r\n ],\r\n imports: [\r\n BrowserModule,\r\n HttpClientModule\r\n ],\r\n providers: [],\r\n bootstrap: [AppComponent]\r\n})\r\nexport class AppModule { }<\/pre>\n
import { Component, OnInit } from '@angular\/core';\r\nimport { HttpClient } from '@angular\/common\/http';<\/pre>\n
@Component({\r\n selector: 'app-root',\r\n templateUrl: '.\/app.component.html',\r\n styleUrls: ['.\/app.component.css']\r\n})\r\nexport class AppComponent {\r\n title = 'app';\r\n constructor(private http: HttpClient){ }\r\n}<\/pre>\n
\n
Using HttpClient To Request\u00a0Data<\/h3>\n
import { Component, OnInit } from '@angular\/core';\r\nimport { HttpClient } from '@angular\/common\/http';<\/pre>\n
@Component({\r\n selector: 'app-root',\r\n templateUrl: '.\/app.component.html',\r\n styleUrls: ['.\/app.component.css']\r\n})\r\nexport class AppComponent implements OnInit {\r\n title = 'app';\r\n results = '';\r\n constructor(private http: HttpClient){\r\n }\r\n\r\n ngOnInit(): void { \r\n this.http.get('https:\/\/api.test.com\/users)\r\n .subscribe(data => { \r\n console.log(data); \r\n }); \r\n }\r\n}<\/pre>\n