3

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

2
  • Possible duplicate of Angular 2 http get not getting Commented Sep 5, 2017 at 8:37
  • Its a little different since I am doing two calls in the same block. I think this question would be useful for others in the future. Commented Sep 6, 2017 at 20:28

2 Answers 2

3

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(/*...*/));
}
Sign up to request clarification or add additional context in comments.

Comments

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

    }

1 Comment

there is no dependency on curly braces, he hasn't subscribed on the second post. Correctly pointed out by @suren

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.