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?
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
.
But subscribe is needed to do some operation after request has been served.I don't have to do anything. Do I still need to subscribe?
Do you have a reference that says that
post()
will not send a request unless there is asubscribe
?@Mandroid
subscribe
is needed in order to get the code executed. Withoutsubscribe
you only create an observable, he will never execute itself. Just try it and you will see the request in the Network tab.@HereticMonkey I have added details to my answer with a source.