HttpClient setup:
Before you start working with HttpClient
in Angular. You need to import HttpClientModule
to your AppModule
.
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {HttpClientModule} from "@angular/common/http";
import {BrowserModule} from "@angular/platform-browser";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
bootstrap: [AppComponent],
})
export class AppModule {
}
Everywhere where you want to use HttpClient
you need to Inject it into constructor
constructor(private http: HttpClient) {}
GET:
For get the method can look something like this. In this example the request URL will look like this http://127.0.0.1:5000/get_nodes?username="jack_list"&nodename="nodeToFind"
const data = {
"username" : "jack_list",
"node_name" : "nodeToFind"
};
const httpOptions = {
params: data,
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Postman-Token': '604243c2-f9da-4f71-b356-a8e31608b45d',
'Cache-control': 'no-cache'
});
this.http.get('http://127.0.0.1:5000/get_nodes', httpOptions);
Post:
For post the method will be very similar you just need to add your data there
const data = {
"username" : "jack_list",
"node_name" : "nodeToFind"
};
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Postman-Token': '604243c2-f9da-4f71-b356-a8e31608b45d',
'Cache-control': 'no-cache'
});
this.http.post('http://127.0.0.1:5000/get_nodes', data, httpOptions);