I am trying to make a HTTP call and my Content-Type is text/plan but I am working with ionic-native/http which only take object and array,here is what I have tried
Error: advanced-http: "data" argument supports only following data types: Array, Object
TestIR(item){
let headers = { 'Content-Type': 'text/plain'};
let sender ='sendir,1:1,0,';
let code = item.keys[10].code;
let body = sender+code;
let url= "";
this.httpNative.setDataSerializer( "json" );
this.httpNative.post(url,body,headers).then(val=>
{console.log(val)}).
catch(e=>{console.log(e)})
}
I am trying to make a HTTP call and my Content-Type is text/plan but I am working with ionic-native/http which only take object and array,here is what I have tried
Error: advanced-http: "data" argument supports only following data types: Array, Object
TestIR(item){
let headers = { 'Content-Type': 'text/plain'};
let sender ='sendir,1:1,0,';
let code = item.keys[10].code;
let body = sender+code;
let url= "http://10.75.0.120/v2/CI00e0148a";
this.httpNative.setDataSerializer( "json" );
this.httpNative.post(url,body,headers).then(val=>
{console.log(val)}).
catch(e=>{console.log(e)})
}
Share
Improve this question
edited Jul 20, 2018 at 14:58
xploshioOn
4,1253 gold badges31 silver badges39 bronze badges
asked Jul 19, 2018 at 7:53
nitishnitish
1112 silver badges15 bronze badges
5
-
What happens when you ment out this line =>
this.httpNative.setDataSerializer( "json" );
? – David R Commented Jul 19, 2018 at 7:54 -
1
Shouldn't content-Type be written as
'text/plain'
notplan
, if that is the type, which you want. – Geshode Commented Jul 19, 2018 at 7:55 - still I get the same error – nitish Commented Jul 19, 2018 at 7:56
- I changed it to plain but still i am getting the same error @Geshode – nitish Commented Jul 19, 2018 at 8:03
-
1
you are setting the serializer to
'json
and i am guessing yourbody
cannot be converted to json as it is not an array. did you try changing the serializer to theutf8
option? or just creating a simple array to include your body and pass it to httpNative? – slashpm Commented Jul 19, 2018 at 12:21
3 Answers
Reset to default 7Assumption: The HTTP plugin being used is cordova-plugin-advanced-http, linked from the Ionic Docs.
Setting the Data Serializer to json tells the plugin to expect a javascript object, but the body you are sending is a string
Solution 1
If the API you are sending data to can handle it, set the Data Serializer to "utf8"
.
this.httpNative.setDataSerializer( "utf8" );
This tells the plugin to expect text.
Solution 2
Change the body to an object before calling POST.
body = JSON.parse(json);
The string you pass to JSON.parse()
must be valid json.
Edit : Removed references to a bug that is now fixed
If you are using the cordova-plugin-advanced-http
you could also do the following if you only want to set the content type for one request.
const options: any = {
serializer: 'utf8',
method: 'post',
data: 'my string value'
};
this.http.sendRequest('https://mysite', options);
If your response type is json
I remend to parse it by yourself by using JSON.parse
.
I was receiving the same error and I tried the @Glen Davies solution, but the error kept and was intermittently. Sometimes it used to work, sometimes not.
Error: advanced-http: "data" argument supports only following data types: Array, Object
I'm using Ionic 3 application with cordova http plugin in the http-interceptor
and after some hours, I've discovered the problem was that even with the serializer being set in the app.ponent.ts
, it was not working.
The solution was moving the:
this.http.setSSLCertMode('pinned');
this.http.setDataSerializer('utf8');
To the constructor inside the HttpInterceptor
.
After this, the error was gone.