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.

9
  • 1
    It looks like you don't actually subscribe to the observable, so the request never gets made. Commented Feb 21, 2019 at 17:27
  • Do I need to subscribe here: this.messageService.send(model).pipe(...)? Commented Feb 21, 2019 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. Commented Feb 21, 2019 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: <Response>> Should it not be just one like this? <Response> Commented Feb 21, 2019 at 17:31
  • 1
    Possible duplicate of Angular 2 http.post() is not sending the request Commented Feb 21, 2019 at 18:31

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

1 Comment

I added an update with the change you mention but still no luck. And it is working with postman.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.