Search code examples
angulartypescripthttppost

how to get http response from typescript post


I have tried looking at other questions but they dont seem to help. I have the following method in typescript

transferAmount(transfer: Transfer): Observable<number> {
return this.http
  .post<number>(
    `${this._baseUrl}:8092/api/transfers/`,
    transfer,
    { observe: "response" }
  )
  .pipe(
    mergeMap((response: any) => {
      console.log(response.json());
      const location = `${response.headers.get("Location")}`;
      console.log(parseInt(location.slice(location.lastIndexOf("/") + 1)))
      return of(parseInt(location.slice(location.lastIndexOf("/") + 1)));
    })
  );

}

I have tried to log the response using

console.log(response.json()) 

but this does not work?


Solution

  • Try to this:

    this.transferAmount(transfer: Transfer).subscribe(resp => JSON.stringfy(resp));
    

    When you do a request the post method return an observable to you, but you need to subscribe in that observable to get the response.