Simple GET request with response type <any>
This sends an HTTP GET request to the npm api for a list of packages that belong to the @angular scope, then assigns the total returned in the response to the local property totalAngularPackages. The response type is set to <any> so it handle any properties returned in the response.
This sends the same request as the above but sets the response type to a custom SearchResults interface that defines the expected response properties.
GET request with error handling
This sends a request to an invalid url on the npm api and logs the error to the console from an error handler function that is passed as the second parameter to the subscribe() method of the Observable returned from the call to http.get().
Before making HTTP requests from your Angular app you need to do a couple of things.
1. Add the HttpClientModule to the imports array of your AppModule like below on lines 3 and
This sends an HTTP GET request to the npm api for a list of packages that belong to the @angular scope, then assigns the total returned in the response to the local property totalAngularPackages. The response type is set to <any> so it handle any properties returned in the response.
this.http.get<any>('https://api.npms.io/v2/search?q=scope:angular').subscribe(data => {GET request with strongly typed response.
this.totalAngularPackages = data.total;
})
This sends the same request as the above but sets the response type to a custom SearchResults interface that defines the expected response properties.
interface SearchResults {
total: number;
results: Array<object>;
}
this.http.get<SearchResults>('https://api.npms.io/v2/search?q=scope:angular').subscribe(data => {
this.totalAngularPackages = data.total;
})
GET request with error handling
This sends a request to an invalid url on the npm api and logs the error to the console from an error handler function that is passed as the second parameter to the subscribe() method of the Observable returned from the call to http.get().
this.http.get('https://api.npms.io/v2/invalid-url').subscribe(Prerequisites for making HTTP request from Angular
data => this.totalAngularPackages = data.total,
error => console.error('There was an error!', error)
)
Before making HTTP requests from your Angular app you need to do a couple of things.
1. Add the HttpClientModule to the imports array of your AppModule like below on lines 3 and
import { NgModule } from '@angular/core';2. Import the HttpClient into your component and add it to the constructor() params like below on lines 2 and 8.
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
HttpClientModule
],
declarations: [
AppComponent
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component, OnInit } from '@angular/core';source : https://jasonwatmore.com/post/2019/09/06/angular-http-get-request-examples
import { HttpClient } from '@angular/common/http';
@Component({ selector: 'app', templateUrl: 'app.component.html' })
export class AppComponent implements OnInit {
totalAngularPackages;
constructor(private http: HttpClient) { }
ngOnInit() {
// Simple GET request with response type <any>
this.http.get<any>('https://api.npms.io/v2/search?q=scope:angular').subscribe(data => {
this.totalAngularPackages = data.total;
})
}
}
No comments:
Post a Comment