【发布时间】:2017-11-26 09:18:57
【问题描述】:
以下 Node.js 代码:
var request = require('request');
var getLibs = function() {
var options = { packages: ['example1', 'example2', 'example3'], os: 'linux', pack_type: 'npm' }
request({url:'http://localhost:3000/package', qs:options},
function (error , response, body) {
if (! error && response.statusCode == 200) {
console.log(body);
} else if (error) {
console.log(error);
} else{
console.log(response.statusCode);
}
});
}();
发送如下接收到的 http GET 请求查询:
{"packages"=>{"0"=>"example1", "1"=>"example2", "2"=>"example3"}, "os"=>"linux", "pack_type"=>"npm"}
我怎样才能优化这个请求,像这样接收:
{"packages"=>["example1", "example2", "example3"], "os"=>"linux", "pack_type"=>"npm"}
注意。 REST API 是在 Ruby on Rails 中构建的
【问题讨论】:
-
我认为这是预期的行为。您将如何通过查询参数中的 get 请求实现发送数组?通常以
?id[]=uuid1&id[]=uuid2的形式发送 -
我已经成功地使用 Ruby 以所需格式发送查询。那么为什么 Ruby 和 Node.js 之间的请求格式会发生变化呢?以及如何阻止它发生?
标签: ruby-on-rails node.js http get