0

On an Angular 7 application I am calling a service:

  this.messageService.send(model).pipe(

    catchError(err => {
      console.log('Error:', err);
      return err;
    }),

    map((response: Response) => {
      console.log(response);
    })

  );

The service method calls an API:

public send(model: Model): Observable<Response> {

  const headers: HttpHeaders = new  HttpHeaders();
  headers.set('Content-Type', 'application/x-www-form-urlencoded');

  console.log(model);

  return this.httpClient.post<Response>>(`send`, model, { headers: headers });

}

I am able to see the model on the Console when I do console.log(model);

But a breakpoint I have on the API does not fire and I don't get any error in catchError.

The API action is ASP.NET Core 2.2. as follows:

[HttpPost("send")]
public async Task<IActionResult> Send([FromBody]Model model) {

  return Ok();

}

NOTE:
On this same application I am able to use httpClient with get method without a problem.

Not sure what I am missing. Any idea?

UPDATE

I changed the Angular service code to:

public send(model: Model): Observable<Response>> {

  const headers: HttpHeaders = new  HttpHeaders();
  headers.set('Content-Type', 'application/json');

  console.log(model);

  return this.httpClient.post<Response>>(`send`, JSON.stringify(model), { headers: headers });

}

Still not working.

This is an Angular problem as I just Posted JSon data with Postman.

It worked using Postman and without any kind authentication / authorisation.

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 1
    It looks like you don't actually subscribe to the observable, so the request never gets made. – Kirk Larkin Feb 21 '19 at 17:27
  • Do I need to subscribe here: this.messageService.send(model).pipe(...)? – Miguel Moura Feb 21 '19 at 17:30
  • 2
    Yeah. `.subscribe()` on the end of that. For what you're doing, you would be better off *only* using `subscribe` but with the `next` and `error` callbacks. – Kirk Larkin Feb 21 '19 at 17:31
  • @Miguel Moura Forgive me If I'm wrong (as I am not two familiar with Angular 7) but is that a typo? the two angle brackets here: > Should it not be just one like this? – Sarah Feb 21 '19 at 17:31
  • @Sarah It is a typo and I corrected but on my Application it is fine as it compiles. It was only when I wrote it on Stackoverflow. – Miguel Moura Feb 21 '19 at 17:33
  • @MiguelMoura No probs. Was just making sure you knew. :) and I think Kirk is right that you need to subscribe. Something like this happened to me recently with Angular 5. – Sarah Feb 21 '19 at 17:34
  • @KirkLarkin Yes, adding subscribe made it work. Could you clarify on an answer what you mean with "For what you're doing, you would be better off only using subscribe". I was using Pipe as in my get methods I am using something like: this.projects$ = this.projectService.getProjects().pipe(map((response: Response) => ... Where this.project& is Observable and I using async on the HTML template. – Miguel Moura Feb 21 '19 at 17:40
  • @KirkLarkin The API after the POST returns two things: a list of errors, if any, or the ID of the message sent which is created on the server. – Miguel Moura Feb 21 '19 at 17:41
  • 1
    Possible duplicate of [Angular 2 http.post() is not sending the request](https://stackoverflow.com/questions/36208732/angular-2-http-post-is-not-sending-the-request) – Igor Feb 21 '19 at 18:31

1 Answers1

1

try see the output window on visual studio. maybe there are some info about the problem. verify if the controller has the AthorizationAttribute. verify the Content-Type', 'application/x-www-form-urlencoded', try change it to 'Content-Type': 'application/json'