0

should I use the httpclient modular in angular to make post calls but in this case the call is not carried out what is due?

app.component.html:

<div class="col-xs-12">
   <button class="btn btn-primary" (click)="getData()">Chiamata rest</button>
</div>

app.component.ts:

import { Injectable,Component, OnInit ,DoCheck} from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';


const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

getData(){
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    return this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions);
  }

1 Answers1

0

There are few things you need to do before actually fixing the issue. You should move the part where you are invoking the api to an angular service.

You have to subscribe to the call if you want it to execute. See the HttpClient documentation.

If you just want to invoke the API in your component, change your code as,

getData(){
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions).subscribe((data) =>{
       console.log(data);
    });
};
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396