silkimen / cordova-plugin-advanced-http

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
392 stars 314 forks source link

Error: advanced-http: header values must be strings #93

Closed javi124 closed 6 years ago

javi124 commented 6 years ago

Hi, I updated from 1.10.2, and and now I get this error. My platform is ios and works with ionic. I think DataSerializer is the problem. Thanks, great job.

silkimen commented 6 years ago

Hi javi124, this message is a new error message which is thrown when you are providing header values which are not string but e.g. number values. Can you provide a minimum example so I can better understand your problem?

javi124 commented 6 years ago

Hi dilkimen, here it is:

savePush (event_id:number, town_id:number, date: Date = new Date()) {

    const headers = new Headers();

    const device: PushNotificationInterface = {

        model: this.settings.settings.model,
        uuid: this.settings.settings.uuid,
        token: this.settings.settings.token,
        platform: this.settings.settings.platform,
        town_id,
        event_id
    };
    const url =  `${AppConfig.apiBasePush}/devices.json`;
    console.log({device});
    this.http.setDataSerializer('json');
    this.http.setHeader("Accept", "application/json");
    this.http.setHeader("Content-Type", "application/json");

    this.http.post(url, device, headers)
     .then(data => { this.update200(data.data, device.event_id, device.town_id, date)

     console.log(data.status);
     console.log(data.data); // data received by server
     console.log(data.headers);
     })
     .catch(error => { this.update404(device.event_id, device.town_id, date, error)

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

Thanks.

silkimen commented 6 years ago

Please do console.log(JSON.stringify(headers, null, 2)); before sending the request and post the message. I'd like to see if the header contains non-string values.

javi124 commented 6 years ago

V1.10.2 (data) { "Accept": "application/json", "Content-Type": "application/json" } v1.11.1 (error) {}

silkimen commented 6 years ago

So your headers object is empty? Are you sure? Why is it different for different versions of this plugin? Where did you put the console log? I need to see the content of the headers object before sending the request not afterwards. Actually I need to know what the result of new Headers() is, so you could also do console.log(JSON.stringify(new Headers())). Atm, I'm not able to reproduce your problem.

javi124 commented 6 years ago

Yes, headers is empty. I are only sendind url and device. In v1.10.2 accept, but in 1.11.1, send the error. now, I are sending this this.http.post(url, device, {}) and its work, thanks.

silkimen commented 6 years ago

Hmm ok, that's interesting. I still think that the value of new Headers() can't be an empty object though.

Closing this, feel free to open a new issue, if this won't solve your problem!

dpinter commented 5 years ago

Hello, calling this.http.post(url, body, {}), cordova crash without error. calling this.http.post(url, body, {Content-Type: "application/json", Accept: "application/json"}), cordova crash without error. calling this.http.post(url, body, new Headers()) I get this error: "Error: advanced-http: header values must be strings". (console.log(new Headers()) prints {} ) calling this.http.post(url, body, ""), cordova crash without error. calling this.http.post(url, body, null), cordova crash without error.

android platform: 7.1.1 cordova-plugin-advanced-http: 2.0.1

Please help me

dpinter commented 5 years ago

There was a conflict between cordova-plugin-advanced-http and uk.co.workingedge.phonegap.plugin.launchnavigator about the OKHTTP version. In particular, I resolved it installing:

cordova plugin add uk.co.workingedge.phonegap.plugin.launchnavigator --variable OKHTTP_VERSION=3.10.0

cordova plugin add cordova-plugin-advanced-http --variable OKHTTP_VERSION=3.10.0

carlosen14 commented 4 years ago

solved using

 import _ from 'lodash';

public post(url: string, params: any, options: any): Promise<any> {
    console.log('POST ', url, params, options)
    if (options.headers) {
      _.forEach(options.headers, (value, key) => {
        this.http.setHeader('*', String(key), String(value))
      });
    }

    return this.http.post(url, params, null)
      .then(resp => options.responseType == 'text' ? resp.data : JSON.parse(resp.data));
  }
priyanka-Sadh commented 4 years ago

console.log(JSON.stringify(new Headers())) Is printing {}

And error is same. What should I do now ? Any solution ?

carlosen14 commented 4 years ago

console.log(JSON.stringify(new Headers())) Is printing {}

And error is same. What should I do now ? Any solution ?

@priyanka-Sadh This is absolutely correct, because headers has no value yet!!

You can pass

// example
options.headers = {
accept: 'application/json'
}

solved using

 import _ from 'lodash';

public post(url: string, params: any, options: any): Promise<any> {
    console.log('POST ', url, params, options)
    if (options.headers) {
      _.forEach(options.headers, (value, key) => {
        this.http.setHeader('*', String(key), String(value))
      });
    }

    return this.http.post(url, params, null)
      .then(resp => options.responseType == 'text' ? resp.data : JSON.parse(resp.data));
  }