Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 consecutive http requests

In my angular data service I am trying to make two http request, with the second request depending on data from the first request. The first request is working fine, but for some reason the second request is never hitting my backend server. I was hoping if someone can tell me if I am doing this correctly or show me what I am doing wrong.

@Injectable()
export class DataService {

  constructor( private http: Http ) { }

public twoRequest() {
    this.http.get(`http://localhost:3000/1st_request`).subscribe((data) => 
      this.http.post(`http://localhost:3000/2nd_request`, {data: data}))
}

edit: I didn't subscribe to the second request. I didn't know you have to subscribe to each request you make even if they are in the same block of code

like image 676
xeroshogun Avatar asked Jun 03 '26 20:06

xeroshogun


1 Answers

You need to subscribe to the http.post also. It will never do a request if you don't subscribe to it.

@Injectable()
export class DataService {

  constructor( private http: Http ) { }

  public twoRequest() {
     this.http.get(`http://localhost:3000/1st_request`).subscribe((data) => 
       this.http.post(`http://localhost:3000/2nd_request`, {data: data}).subscribe(/*...*/));
}
like image 138
Suren Srapyan Avatar answered Jun 05 '26 11:06

Suren Srapyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!