Rest-Client Ruby Gem:帮助创建带有标头,查询和设置代理的获取请求 [英] Rest-Client Ruby Gem: Help creating get request with Headers, Query, and setting a proxy
问题描述
到目前为止,这是我想出的.
So far this is what I've come up with.
RestClient.proxy = ENV("http_proxy")
RestClient.head("Cookie","accessToken=#{@GottenTokenString}") #this line not working
resp = RestClient.get("http://someServer/path/RestAPIPage.json",{:params => {:count => @count, :userCount => @userCount}})
我在这里遇到的问题是我似乎无法在get请求中添加任何标头.我正在调用的API要求获取令牌并将其设置为Cookie标头.似乎REST API无法处理此问题.
The problem I have here is that I can't seem to add any headers to the get request. The API I am calling requires that a token be obtained and set to a cookie header. Seems that the REST API can't handle this.
我之所以决定使用RESTCLIENT gem是因为NET :: HTTP.get不允许使用允许添加查询的方法.总而言之,我无法使用RESTCLIENT添加标头.但是我无法使用NET :: HTTP添加查询.
The reason I've decided to use this the RESTCLIENT gem is because the NET::HTTP.get does not allow the usage of methods that allow adding a query. Summarizing, I can't add headers with RESTCLIENT. But I can't add queries with NET::HTTP.
我真的很沮丧.
推荐答案
旧问题,但是添加了.02作为参考,因为我只是在经历同样的事情...
Old question, but adding my .02 for reference since I just struggled through the same...
就我而言,我想在GET请求的标头中传递令牌,并添加参数.诀窍是像标头一样包含标头,但不将其包含在params哈希中.在我的示例中,"Authorization" =>"Bearer#{token}"是标题信息.
In my case, I wanted to pass a token in the header of a GET request, and also add params. The trick was to include the header like a param, but not include it in the params hash. "Authorization" => "Bearer #{token}" is the header info in my example.
require 'json'
require 'rest-client'
JSON.load(RestClient.get("http://yourUrlGoesHere.com", {"Authorization" => "Bearer #{token}", :params => {:foo => 'bar'}}))
这篇关于Rest-Client Ruby Gem:帮助创建带有标头,查询和设置代理的获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!