-2

I am developing ionic app for android. I am making http request globally and based response I am processing that request to page. Example, If response not contains auth key in header I will send error message in globally declared service class.

here page subscriber method-

this.loginService.login(params)
  .subscribe(resp => {
      //processing response
   },
    error => {
      this.alertService.presentToast(AppConstants.NETWORKERRMSG);
      this.loading.dismiss();
    }

middle (login-Service)service to call http service-

         login(params){
          return this.netHelper.post('/method',
          { params } ) }

and global http service method in netHelper class-

  post(url: string, params: any) {
   var response: any ;
    this.http.post<any>(url, params,
     {headers})
        }).subscribe(data => {
           console.log(data.status);
           console.log(data);
           response = data;

        },
    error => {
      console.log(error.status);
      console.log(error.error); // error message as string
      console.log(error.headers);

    });
    return response;
  } 
sachin
  • 153
  • 4
  • 18

1 Answers1

-2

try to change the post method in the netHelper class like this , it will work as expected-

   let respbody: any = '';
   post(url: string, params: any) {

    return this.http.post(url, params,
      {
        headers: headers,
        observe: 'response'
      }).
      map(data => {
        let respbody: any = '';
        let resp: any = data;
        if (resp.status == '200') {
            respbody = resp.body;
        }
        else if (resp.status === '401') {
          this.alertService.presentToast(SECURITYMSG);
        }
        else {
          this.alertService.presentToast(NETWORKERRMSG);
        }
        return respbody;
      });
  }
sachin
  • 153
  • 4
  • 18
  • You've answered a different question to the one you asked. The question you asked was about why your poorly set up observables weren't working. In your own answer to your own question, you've also answered a question about getting headers from the response. And you've still managed to not pass `headers` into the method in your own "fix". And this won't work how you want it to. You need to catch error responses like 401. And `map` should be used for just mapping - side-effects should happen in `tap`. – Kurt Hamilton Mar 05 '20 at 06:49
  • please tell me what is side effects while tap, also there is no other option, because I need to validate response in network class only , otherwise I will validate in each page – sachin Mar 05 '20 at 07:45
  • OK, so now you've deleted the text i was referring to, which was: "but, page subscriber call ends before http request call in globally declared method" – Kurt Hamilton Mar 05 '20 at 07:47
  • actually , that time I am using observer technique to call back, but that did not happen. So I have resolve like this – sachin Mar 05 '20 at 07:48
  • please tell me problem while tap – sachin Mar 05 '20 at 07:49
  • My original answer highlighted why your `post` function would **never** work. You're not using the observer technique, you were **always** returning the equivalent of`Observable.of(undefined)`. And the actual response from `http.post` would never make it out of the function. The fact that you couldn't read your response headers was irrelevant - because the response itself would never, ever, be returned. You can't return a value out of a subscription. And by the time the post response returns, there is no point setting `response = resp`. – Kurt Hamilton Mar 05 '20 at 07:57
  • I showed you how to return a value from an observable. There is **a lot** of documentation on this around the internet. I would recommend reading up on how to return values from an observable. And then you can worry about how to read headers from your response. – Kurt Hamilton Mar 05 '20 at 07:58