Accessing JSON Request Parameters in Sails

In my controllers and other scopes where there is an object req

, I can access the request parameters with req.params('username')

. This is fine for normal POST data, but I want my API to accept a JSON object in the request body and convert it to parameters that I can still get with req.params()

.

So, for example, if I send this as a POST request body to my controller action:

{'username': 'Chris', 'password': 'mypass'}

      

I want to get username and password using req.params('username')

and req.param('password')

.

At the moment, the only thing that works is to send data like this:

username=Chris&password=mypass

      

Any ideas?

+3


source to share


3 answers


It turns out I just need to send the request body as application/json

, not the default.



+4


source


req.query works for me when I send get request with parameters like



/ request param1 = a &? Param2 = b p>

+1


source


You can get it from req.body

. See http://sailsjs.org/#/documentation/reference/req/req.body.html

0


source







All Articles