-2

i want to POST data to two API_URL.

let say

API1 = api/v1/data1
API2= api/v1/data2

I want to GET STATUS CODE 200 for API1 before i post another data for API2.

How can i do that in Angular2?

MunirohMansoor
  • 355
  • 1
  • 10
  • 19
  • 1
    Possible duplicate of [Angular 2: Two backend service calls on success of first service](https://stackoverflow.com/questions/36712659/angular-2-two-backend-service-calls-on-success-of-first-service) – eko May 31 '17 at 05:57
  • have you added header in your http post ? – Ghanshyam Singh May 31 '17 at 06:26

1 Answers1

0

A common function to send the HTTP Request. It will return the response status along with the response data as json:

sendRequest(yourRequest: Request) {
    return this.http.request(yourRequest)
        .map((res: Response) => {
            return { status: res.status, json: res.json() }
        }).catch(err => this.tempError(err));
}

Your code to send two requests:

sendRequest(yourFirstRequest).subscribe(data => {
    if(data.status == 200) {
        sendRequest(yourSecondRequest).subscribe(data => {
            //handle the response from the second request.
        })
    }
})
mridula
  • 3,203
  • 3
  • 32
  • 55