Closed javi124 closed 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?
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.
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.
V1.10.2 (data) { "Accept": "application/json", "Content-Type": "application/json" } v1.11.1 (error) {}
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.
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.
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!
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
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
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));
}
console.log(JSON.stringify(new Headers())) Is printing {}
And error is same. What should I do now ? Any solution ?
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)); }
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.