I'm trying to make a PUT and/or POST request from Node.js to my Rails 3 server. I am passing my parameters in the body but they are not getting turned into the params hash in the Rails controller. My code is as follows:
http = require('http');
request = require('request');
qs = require('qs');
data = qs.stringify({name: 'Aaron', points: 10});
request_opts = {
uri: 'http://localhost:3000/users/update',
method: 'PUT',
body: data
}
request(request_opts, function() { console.log(arguments) });
I put a debugger in my Rails controller and params is nil but response.body.read
returns the string I have in data
.
How can I make a PUT / POST request from Node.js where the parameters are compatible with Rails?
The querystring
module will serialize your data as a parameter encoded form. By default rails expects a JSON serialized entity. You should use JSON.stringify
instead of qs.stringify
to serialize the your user into the request body. Alternatively, you can explicitly specify a content-type header of application/x-www-form-urlencoded
to tell rails the type of the body you are sending.