GET请求在Ruby代码中成功但在Node.js中失败

时间:2017-06-22 19:23:05

标签: javascript ruby node.js http get

我使用Ruby编写了以下代码。以下代码行

puts WhatDepsService.new(packages, os)

调用实例化WhatDepsService calss并发送以下查询

{packages: ['example1', example2, 'example3'], os: 'linux', pack_type: 'gem'} 

在http GET请求中,并使用代码200

成功收到响应
require 'httparty'
require 'json'

class WhatDepsService
    include HTTParty

    base_uri 'http://localhost:3000'

    def initialize()
        @options = {query: {packages: ['example1', 'example2', 'example3'], os: 'linux', pack_type: 'gem'}}
        @packages = load_deps(@options)
    end

    private

    def load_deps(options)
        begin
            deps = self.class.get("/package", options)
            JSON.parse(deps.to_json)
        rescue
            abort "Sorry, there's a problem connecting to our server"
        end
    end
end

现在我使用node.js编写了这个代码,它应该发送相同的http GET请求:

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);
        }
    });
}();

但是node.js中的请求收到响应代码422(Unprocessable Entity)。我该如何解决这个问题?

更新

API从Ruby接收请求,如下所示:

{"packages"=>["example1", "example2", "example3"], "os"=>"linux", "pack_type"=>"gem"}

但是收到来自Node.js的请求如下:

{"packages"=>{"0"=>"example1", "1"=>"example2", "2"=>"example3"}, "os"=>"linux", "pack_type"=>"npm"}

0 个答案:

没有答案