6

I've got this httpclient request in an angular 4.3 service:

updateTermin(termin: Termin){
  let url = '${this.termineUrl}/${termin.id}';
  this.http
  .put(url, termin, {headers: this.headers})
}

it only gets fired if i append a .subscribe() like this:

updateTermin(termin: Termin){
  let url = '${this.termineUrl}/${termin.id}';
  this.http
  .put(url, termin, {headers: this.headers})
  .subscribe()
}

The other request (delete or post) work without append .subscribe(). Why?

2
  • 1
    No, they don't. You need to subscribe for a HTTP request to be sent, whatever the method is. Commented Oct 24, 2017 at 17:16
  • 1
    It cant be, you need subscribe Commented Oct 24, 2017 at 17:16

1 Answer 1

19

The HttpClient request methods (all of them) return an Observable: basically it's a chain of commands waiting to be executed. Nothing happens until you call subscribe() on that chain. So this.http.put(...) only prepares the commands for you.

If you're coming from an environment that uses promises, this will seem counter-intuitive because a promise executes as soon as it comes alive. Observables are different. Check out this short intro on YouTube

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

2 Comments

spot on, never knew about waiting on the call to subscribe!
Almost half day digging this issue.. Thanks @BeetleJuice for the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.