Warm tip: This article is reproduced from serverfault.com, please click

Angular: Sending POST request with empty request body

发布于 2020-11-30 16:48:26

I am making a angular POST call to a backend service. Usecase is such that request has no body, so I do like :

http.post("my/backend/service", null);

I observe that endpoint is not hit, and there is also no error.

Is this not the correct way of handling empty request body for POST?

Questioner
Mandroid
Viewed
0
Marco 2020-12-01 16:50:23

post method return an Observable so you need either to subscribe to the post method or return the Observable and subscribe to your method. Calling the post method and getting the Observable is like preparing the request, subscribing to the Observable is like runing the query.

http.post("my/backend/service", null).subscribe();

You can find more about it in the last section of this Angular tutorial.

Because the service method returns an Observable of configuration data, the component subscribes to the method's return value.

The example use the get() method, but the problem come from the type of the response: Observable.