user3809938
user3809938

Reputation: 1304

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?

Upvotes: 0

Views: 2171

Answers (2)

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.

Upvotes: 1

Toon
Toon

Reputation: 66

You can get the answer when you subscribe to your observable like this :

transferAmount(..).subscribe(res => console.log(res));

Upvotes: 0

Related Questions