简体   繁体   English

与红宝石的restclient

[英]restclient with ruby

Here i am trying to pass one ID with the url, but that ID didn't append with URL... 在这里我试图通过URL传递一个ID,但是该ID没有随URL附加...

def retrieve
    url =  "http://localhost:3000/branches/"
    resource = RestClient::Resource.new url+$param["id"]
    puts resource
end

giving ID via commend line that is 通过推荐行给出ID

ruby newrest.rb  id="22"

I have got the error like this 我有这样的错误

`+': can't convert nil into String (TypeError)

But all this working with mozilla rest client. 但是所有与mozilla rest client一起工作的东西。 How to rectify this problem? 如何纠正这个问题?

像这样:

RestClient.get 'http://localhost:3000/branches', {:params => {:id => 50, 'name' => 'value'}}

From my point of view line puts resource seems strange, but when we leave it as it is I'd suggest 从我的观点来看, puts resource似乎很奇怪,但是当我们将其保留时,我建议

def retrieve
    url =  "http://localhost:3000/branches/"
    resource = RestClient::Resource.new url
    res_with_param = resource[$param["id"]]
    puts res_with_param
end

I haven't tried so there may be a syntax mistakes. 我没有尝试过,所以可能存在语法错误。 I'm really newcomer in ruby. 我真的是红宝石的新手。 But idea is good I hope. 但是我希望这个主意很好。

Greetings, KAcper 问候,凯珀

Here are some examples from the documentation: 以下是文档中的一些示例:

private_resource = RestClient::Resource.new 'https://example.com/private/resource', 'user', 'pass'

RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }

Just experiment with comma-separated parameters or with hashes so see what your URL gives you. 只需尝试使用逗号分隔的参数或哈希值,即可查看您的URL给您带来了什么。

You can find the command line parameters in the global ARGV array. 您可以在全局ARGV阵列中找到命令行参数。

If ruby newrest.rb 22 will do then just 如果ruby newrest.rb 22会做

id = ARGV[0]
response = RestClient.get "http://localhost:3000/branches/#{id}"
puts response.body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM