Error: advanced-http value must be string


#1

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

  });

#2

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


#3

and how can I fix it?!, I can’t change the backend


#4

I have no idea what “the 401 polyfills error” means.


#5

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

      };


#6

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).


#7

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


#8

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


#9

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

or for ionic 3 refer this


#10

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: