Error: advanced-http value must be string

have try to user @angular/http to send my api but I got the 401 polyfills error so I change it to @ionic-native/http , but then I got yet another error which is :Error: advanced-http value must be string" so I log my header and it was empty???

The log

"normalizedNames": {},
  "lazyUpdate": [
    {
      "name": "Authorization",
      "value": "Basic xxxxxxxx",
      "op": "a"
    },
    {
      "name": "Content-Type",
      "value": "application/x-www-form-urlencoded",
      "op": "a"
    }
  ],
  "headers": {},
  "lazyInit": {
    "normalizedNames": {},
    "lazyUpdate": null,
    "headers": {}
  }

mycode

import { HTTP } from '@ionic-native/http';

 constructor(public http: HTTP) {}

  login(username, password) {

    let body = {
      username: username,
      password: password
    };
    let headers = new HttpHeaders();
    headers = headers.append('Authorization', 'Basic xxxxxx');
    headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(apiUrl, body, { headers: headers })
  .then(data => {

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

  })
  .catch(error => {

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

  });

Letā€™s fix that, because you shouldnā€™t be using ionic-native for this, but rather Angularā€™s HttpClient.

and how can I fix it?!, I canā€™t change the backend

I have no idea what ā€œthe 401 polyfills errorā€ means.

I was using the angular HttpClinet ,but it did not work

let body = {
      grant_type: 'password',
      username: username,
      password: password
    };
    let headers = new HttpHeaders();
    headers = headers.append('Authorization', 'Basic xxxxxx');
    headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
    console.log(JSON.stringify(headers, null, 2));

    return this.http.post('apiurl', body, { headers: headers })
      .subscribe(data => {
this.data = data
        console.log(data);

      })
      err => {

        console.log(err); // error message as string

      };

That looks like a CORS issue that really needs to be fixed on the server, but you shouldnā€™t be using raw HTTP in any event, and especially not when doing security stuff. You should be using HTTPS, with real hostnames and real certificates (no numeric IP addresses).

alright how can I bypass CORS , without change the backend because I can not change it I have also try to test on my device but the same thing keep on happening

Did you find any workaround on this? I am facing the same issue

i recommend to use native http.
if you are working on ionic 4. refer this doc

or for ionic 3 refer this

1 Like

I refer it https://ionicframework.com/docs/native/http
getProfile(){

return this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + '_api_care/my/profile'), {},{headers:this.getMcsHeaders()});

}
I still got error . :frowning:

headers should be an object whose values are stringsā€¦
when u assign headers as {headers:this.getMcsHeaders()}, you are assigning object as header value that is throwing the error.
Try
this.http.get(this.mcs.mobileBackend.getCustomCodeUrl(mcsConfig.environment + ā€˜_api_care/my/profileā€™), {},this.getMcsHeaders())

1 Like

Thank you. I got it. <3