When I POST data to a custom route in RhoConnect, the request params use the content of the POST body as the key and null as the value. As in:
'{ "data": "test" }': null
Even if I change the body content to be just text like "test 1234", the params come back as:
'test 1234': null
The body content is always set to the key instead of the value for the key/value params. This makes it very difficult, if not impossible to get that content in RhoConnect as the body value will always change (basically a "dynamic" key).
Here's my example code:
Mobile client:
Rho.Network.post({
url: 'http://localhost:9292/rc/v1/app/regActionLogin',
body: {
data: "test"
}
}, function(e) {
if (e.http_error == "200") {
var user = JSON.parse(e.body);
console.log('regAction response', user);
self.router.navigate('login', {trigger: true});
} else {
console.log('e', e);
Rho.Notification.showPopup({
title: 'Bad Login',
message: 'Your username/password were incorrect. Please try again.',
icon: ":alert",
buttons: [{id: 'yes', title: 'OK'}]
}, function() {
$('#rho-password').val('');
});
}
});
Rhoconnect application_controller.js:
app.post('/regActionLogin', {'login_required':false, "source_required":false, "client_required":false}, function(req, resp){
console.log('req.params', req.params);
resp.send(true);
});
Log from RhoConnect:
[7344][08:10:44.799 AM 2014-06-04] Rhoconnect Server v4.1.0 started...
[7344][08:10:44.916 AM 2014-06-04] Starting Node.js process: 3536
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:9292, CTRL+C to stop
req.params { '{ "data": "test" }': null,
source_name: 'app',
source_id: 'app' }
I'm using Rho 4.1.0 and Rhoconnect 4.1.0 on Windows 7 testing with the Rhosimulator. If I'm sending the data incorrectly or missed a step, any help would be appreciated.
-Mark
1 Replies
Mark,
Took me a while (and I probably should have told you I was working on your bug), but I finally figured out what was going on.
You need to add a header, which includes the content type and the mime type. See the change that i made to your code:
Rho.Network.post({
url: 'http://192.168.1.116:9292/rc/v1/app/regActionLogin',
headers: {"Content-Type":"application/json"},
body: { "stuff": "test" },
},
function(e)
{
if (e.http_error == "200")
{
var user = JSON.parse(e.body);
console.log('regAction response', user);
self.router.navigate('login', {trigger: true});
}
else
{
console.log('e', e);
Rho.Notification.showPopup({
title: 'Bad Login',
message: 'Your username/password were incorrect. Please try again.',
icon: ":alert",
buttons: [{id: 'yes', title: 'OK'}]
}, function() { $('#rho-password').val(''); });
}
});