2016-12-16 31 views
0

我正在尝试将使用http gem的终端中的curl请求转换为ruby代码。无法使用http ruby​​创建api请求gem

这是卷曲的要求,让我回了有效的JSON我想:

curl -X GET --header 'Accept: application/json' --header 'api_key: somekey' --header 'authtoken: sometoken' 'https://cdn.domain.io/v3/content_types/shirts/entries?environment=dev' 

随着http gem我尝试做这在我的Ruby脚本:

HTTP.headers(:Accept => "application/json", :api_key => 'somekey', :authtoken => 'sometoken').get("https://cdn.domain.io/v3/content_types/shirts/entries", :params => { :environment => 'dev'}).body.readpartial 

这给我的返回"api_key":["is not valid."]}服务器出错

我在做什么错?我如何得到这个工作?

+0

可能做出不同的'.header(...)'调用(连锁他们)像'.headers(接受: “应用/ JSON”)。头(api_key:'somekey')....'?我不知道http宝石;如果这个apporach工作,可能值得在github上签署一个bug报告。 – Felix

+0

像你说的那样尝试链接。没有工作 –

+0

没有得到与WEBrick v1.3.1,http v2.1.0和Rails v4.2.5的错误。也许发布有关如何重现的更多信息? –

回答

0

Typhoeus似乎是工作进行的顺利:

require "typhoeus" 
require 'multi_json' 
require "awesome_print" 

response = Typhoeus::Request.new(
    "https://api.domain.io/v3/content_types/shirts/entries?environment=dev", 
    headers: { api_key: "somekey", access_token: "sometoken", 
    accept_encoding: "gzip" } 
).run 

# puts response.body 
begin 
    ap MultiJson.load(response.body, :symbolize_keys => true) 
rescue MultiJson::ParseError => exception 
    p exception.data # => "{invalid json}" 
    p exception.cause # => JSON::ParserError: 795: unexpected token at '{invalid json}' 
end