SailsJS: POST请求在调用req.param()和req.file()时无法正确解析

SailsJS: POST requests are not resolved properly when invoking req.param() and req.file()

本文关键字:req file 调用 POST param SailsJS 请求      更新时间:2023-09-26

我使用PostMan来测试我的控制器,它通过使用req.param()和req.file()从POST请求中读取数据。奇怪的是,我提交的参数顺序很重要。例如,

案例1:

file: some file attached
property1: 1

文件将被正确解析,而property1不能(undefined)。

案例2:

file: some file attached
property1: 1
property2: 2

req.param('property2')对于第一个请求将返回1(是的,1,这里没有打字错误),对于随后的请求将返回未定义。

案例3:

property1: 1
file: some file attached

这样,一切都很好。

是预期行为还是bug?

使用Skipper (Sails中的文件上传系统),您需要在文件参数之前发送所有文本参数。例子中的第三种情况应该是你总是如何发送请求的。有关更多信息,请参阅Skipper文档:

https://github.com/balderdashy/skipper text-parameters

req.param()为PATH参数。我猜你是想在这里获得post数据,所以你应该使用req.body('key')