amtest
amtest

Reputation: 700

restclient with ruby

Here i am trying to pass one ID with the url, but that ID didn't append with 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

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. How to rectify this problem?

Upvotes: 0

Views: 1761

Answers (4)

Kacper Perschke
Kacper Perschke

Reputation: 179

From my point of view line puts resource seems strange, but when we leave it as it is I'd suggest

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

Upvotes: 0

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

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.

Upvotes: 0

Colin MacKenzie - III
Colin MacKenzie - III

Reputation: 1393

Like this:

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

Upvotes: 1

Jonas Elfström
Jonas Elfström

Reputation: 31428

You can find the command line parameters in the global ARGV array.

If ruby newrest.rb 22 will do then just

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

Upvotes: 0

Related Questions