cordovaHTTP.post not working as expected
Posted: 21:11, 01 Mar 2016
Hello All,
Apologies if this kind of question is asked earlier. I tried searching but was not successful.
I copied the code of 'TI SensorTag CC2650 & CC2541 Sensors' and trying to enhance it.
I wanted to send the temperature data from sensor to an oData service being hosted on SAP Hana Cloud Platform.
The following is the my code. For now I hardcoded the values.
Note: I replaced the 'Bearer' code with all 'x....'
------------------------------------------------------------------------------------------------------------------------
cordovaHTTP.setHeader("Content-Type", "application/json;charset=utf-8" , function() {
alert(response.status);
}, function() {
alert(response.status);
});
cordovaHTTP.post("https://iotmmsi074490trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/5432d276-2573-4edc-9b47-0c1a26438db6",
{ "mode": "async","messageType": "6988e4f679b44b20a6f8","messages": [{"temperature": "23.00","humidity": "64.00"}] },
{Authorization : "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}, function(response) {
// prints 200
alert(response.status);
try {
response.data = JSON.parse(response.data);
// prints test
alert(response.data.message);
} catch(e) {
alert("JSON parsing error");
}
}, function(response) {
// prints 403
alert(response.status);
//prints Permission denied
alert(response.error);
});
------------------------------------------------------------------------------------------------------------------------
I always get a response 400. I have always trouble with JSON string. My server always seems to receive JSON string like below i.e., the formatting and quotes(") were lost.
messageType=6988e4f679b44b20a6f8&messages[][humidity]=64.00&messages[][temperature]=23.00&mode=async
Can you please guide me and suggest what I am missing.
I tried the same posting using postman client and it is working.
Thank you.
- Subhakanth
Apologies if this kind of question is asked earlier. I tried searching but was not successful.
I copied the code of 'TI SensorTag CC2650 & CC2541 Sensors' and trying to enhance it.
I wanted to send the temperature data from sensor to an oData service being hosted on SAP Hana Cloud Platform.
The following is the my code. For now I hardcoded the values.
Note: I replaced the 'Bearer' code with all 'x....'
------------------------------------------------------------------------------------------------------------------------
cordovaHTTP.setHeader("Content-Type", "application/json;charset=utf-8" , function() {
alert(response.status);
}, function() {
alert(response.status);
});
cordovaHTTP.post("https://iotmmsi074490trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/5432d276-2573-4edc-9b47-0c1a26438db6",
{ "mode": "async","messageType": "6988e4f679b44b20a6f8","messages": [{"temperature": "23.00","humidity": "64.00"}] },
{Authorization : "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}, function(response) {
// prints 200
alert(response.status);
try {
response.data = JSON.parse(response.data);
// prints test
alert(response.data.message);
} catch(e) {
alert("JSON parsing error");
}
}, function(response) {
// prints 403
alert(response.status);
//prints Permission denied
alert(response.error);
});
------------------------------------------------------------------------------------------------------------------------
I always get a response 400. I have always trouble with JSON string. My server always seems to receive JSON string like below i.e., the formatting and quotes(") were lost.
messageType=6988e4f679b44b20a6f8&messages[][humidity]=64.00&messages[][temperature]=23.00&mode=async
Can you please guide me and suggest what I am missing.
I tried the same posting using postman client and it is working.
Thank you.
- Subhakanth