[英]RestClient get with auth_token
I am new to RestClient, but I deeply searched in the Web and I couldn't find something helping. 我是RestClient的新手,但是我在网络上进行了深入搜索,找不到任何帮助。
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. 并且我喜欢它,但是我不知道在哪里添加AUTH_TOKEN和其他参数。 I already tried to add auth_token inside the headers and apart but inside the initialization. 我已经尝试将auth_token添加到头文件中以及初始化之外。
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 我必须将参数和auth_token(也被视为参数)放在: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. 其中par是参数的哈希。 In my case par = {:states = "fail"}
就我而言, par = {:states = "fail"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.