0

I would like to retrieve data from API to ionic. First, I was facing issue of CORS and unable to retrieve data from different devices except original port. I was trying to use http native to retrieve data on device. I have a problem of getting header from API. I am not allowed to modify server. However, I ended up with error as below : Please guide me how to use http native in ionic.

 (in promise): Error: advanced-http: header values must be strings
Error: advanced-http: header values must be strings

I have tried with following code:

login(username: String, password: String) {
    if(username === undefined || username === '' || password === undefined || password === null){
      presentAlert("No username or password", this.alertController);
      return;
    }

      this.mcs.mobileBackend.setAuthenticationType(this.mcs.AUTHENTICATION_TYPES.oauth);
      this.mcs.mobileBackend.authorization.authenticate(username, password).then(
        () => {
          console.log("Test");
          this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + '_api_care/lovs?code_table_name=activity_code'), {}, {headers:careheader})
      .then(data => {

        console.log(data.status);
        console.log(data.data); // data received by server
        console.log(data.headers);

      })
      .catch(error => {

        console.log(error.status);
        console.log(error.error); // error message as string
        console.log(error.headers);

      });
 }
      ).then(() => {
        console.log('test2' + username + password);
        this.getUserRole();
        this.authenticationState.next(true);
      }).catch(err => {
        console.log(err,Headers);
        presentAlert(err, this.alertController);
      }); 

  }
getHeader(){
    let headers = new Headers();
    this.createAuthorizationHeader(headers);
    headers.append('Content-Type', 'application/json');
   }
getUserRole() {
    return this.http.get(this.mcs.mobileBackend.getPlatformUrl('users/me'), {},{ }).then(data => {
      console.log(data); console.log(data.headers);
    });
  }  
 getReportList(offset: number = 0, limit: number = mcsConfig.rowLimit) {
    let queryParam = '?offset=' + (offset * limit) + '&limit=' + limit;
    //return this.mcs.mobileBackend.customCode.invokeCustomCodeJSONRequest(mcsConfig.environment + '_api_care/my/incidents' + queryParam, 'GET', null);
    return this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + '_api_care/my/incidents'+ queryParam), {},{headers: this.getMcsHeaders()});
    }

The data is not shown.

9
  • can you make your question clean and simple? Commented Apr 15, 2019 at 4:41
  • because i am using native http it working great but your question is little complicated to understand. Commented Apr 15, 2019 at 4:43
  • @KhurshidAnsari May I know how to use http native it ? Is it correct the way i use http native? Thanks a lot. Commented Apr 15, 2019 at 4:48
  • I think you are not passing url in string data type. Commented Apr 15, 2019 at 4:52
  • @KhurshidAnsari my report list url is not correct? it works in port8100. but not in Emulator. How to pass url in string datatype. Thanks a lot Commented Apr 15, 2019 at 4:57

1 Answer 1

1

Here is my header below :

getMcsHeadersString() {
var cheaders = {};
for (let key in this.mcs.mobileBackend.getHttpHeaders()) {
  cheaders[key]= this.mcs.mobileBackend.getHttpHeaders()[key];
}
//console.log("STring_HeaderKey_"+cheaders['_keys']+"HeaderValue_"+cheaders['_values']);
delete cheaders['_keys'];
delete cheaders['_values'];
return cheaders;

}

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

Comments

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.