简体   繁体   中英

Accessing JSON request parameters in Sails

In my controllers and other areas where there's a req object, I can access request parameters using req.params('username') . This is fine for normally POSTed data, but I want my API to accept a JSON object in the request body and convert it to parameters that I can still access with req.params() .

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

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

I want to be able to get the username and password using req.params('username') and req.param('password') .

At the moment the only thing that works is sending the data like this:

username=Chris&password=mypass

Any ideas?

结果我只需要将请求体发送为application/json而不是默认值。

req.query is working for me when I send a get request with parameters as

/request?param1=a&param2=b

You should be able to get it from req.body . See http://sailsjs.org/#/documentation/reference/req/req.body.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM