Home > Enterprise >  HttpClient.post is successful but Rest API is not getting the data
HttpClient.post is successful but Rest API is not getting the data

Time:09-30

I am trying to store data from my angular app into database. I am using HttpClient to POST data to the RestAPI. It is sending the the request but API is not getting data and returns response code:500

I have tried logging the data to console and I can see the data but for some reason RestAPI is not getting it.

I have tried sending post request with postman and It works, So I doubt there is any problem with backend.

CodePudding user response:

You need subscribe after post function.

this.http.post(`${this.Baseurl}/insertPost`, newPost, this.httpheader).subscribe(
      res => {
         // do something
      },
      err => {
        // do something
      }
);

CodePudding user response:

  • If you're not logging anything it's because you need to call .subscribe() or .toPromise().
  • If you manage to log something, but the response is null and returned an status code of 500, then the API crashed (had a runtime error) and your request is likely to have been cancelled

CodePudding user response:

As @YuTing mentioned you need to subscribe after calling a post method.

this.http.post(`${this.Baseurl}/insertPost`, newPost, this.httpheader)
.subscribe((response: any) =>{

//Here response is the JSON response sent by the server when the API call made.
      if(response){

         //do something

      }
});
  • Related