I've just worked around a strange bug. I'm sending in a REST PUT call with valid json and an application/json header.
The json in the PUT call is
{
"gravatarURL": "http://www.gravatar.com/avatar/?d=mm"
}
Any ideas on why valid json is not being recognised in req.params?
The code that handles the workaround parses the json body into params is
updateProfile: function(req, res) {
tag = 'UserController.updateProfile' ;
user = {} ;
user.id = req.session.userId ;
if (!user.id){
user.id = 0 ;
}
/* the following line of code should result in params with elements */
params = req.params ;
/* what I get is params == [] */
/* this is the start of the workaround */
if ( params.length == 0) {
try {
/* copying the body creates a valid json object */
params = {}
params.body = req.body ;
params = params.body ;
/* gravatarURL is the parameter sent in via the REST PUT call */
user.gravatarURL = params.gravatarURL ;
} catch (e){
/* ignore and pass through to error with no params */
console.log( tag + '.error: ' + e.message ) ;
}
} else {
/* this is what I expect to be able to do */
user.gravatarURL = req.param['gravatarURL'] ;
}
if ( user.id && user.gravatarURL) {
console.log( tag + '.update.start' ) ;
User.update({ id: user.id }, { gravatarURL: user.gravatarURL }, function(error, updatedUser) {
if (error) {
console.log( tag + '.update.error: ' + error.message ) ;
return res.negotiate(error);
} else {
console.log( tag + '.update.finish: ' ) ;
return res.json(updatedUser);
}
});
} else {
if ( !user.id ) {
error = {} ;
error.message = 'authorisation required. please login.' ;
return res.badRequest({error:error}) ;
}
if ( !user.gravatarURL ) {
error = {} ;
error.message = 'gravatarURL: required' ;
return res.badRequest({error:error}) ;
}
}
} ,