I am new to RestClient, but I deeply searched in the Web and I couldn't find something helping.

My code is the following, and is working, but I'm searching for something more elegant:

def get_from_mgmt(sub_path, par)

  par += "&" unless par.empty?
  path = ":http//#{USER}:#{PASSWORD}@#{HOST}/#{sub_path}.json?#{par}auth_token=#{AUTH_TOKEN}"
  single_page = JSON.parse(RestClient.get path)

end

I found in internet the following:

response = RestClient::Request.new(
:method => :get,
:url => @my_url + "/" + path.to_s,
:user => @my_user,
:password => @my_pass,
:headers => { :accept => :json,
:content_type => :json }
).execute
results = JSON.parse(response.to_str)

and I like it, but I don't understand where to add AUTH_TOKEN and other parameters. I already tried to add auth_token inside the headers and apart but inside the initialization.

Any help is welcome! Thank you.

有帮助吗?

解决方案

Ok, finally I found it. I have to put the parameters and the auth_token (that is also treated as parameter) in :payload

response = RestClient::Request.new(
     :method => :get,
     :url => base_url + "/" + sub_path,
     :user => user,
     :password => pwd,
     :headers => {:accept => :json,
                  :content_type => :json},
     :payload => {:auth_token => auth_token}.merge(par)
).execute

where par is a hash of parameters. In my case par = {:states = "fail"}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top