Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestClient.get should allow for header and params #397

Closed
MrHubble opened this issue Jun 9, 2015 · 5 comments
Closed

RestClient.get should allow for header and params #397

MrHubble opened this issue Jun 9, 2015 · 5 comments

Comments

@MrHubble
Copy link

MrHubble commented Jun 9, 2015

From the docs we have:

Request objects know about query parameters and will automatically add them to the URL for GET, HEAD and DELETE requests, escaping the keys and values as needed:

RestClient.get 'http://example.com/resource', :params => {:foo => 'bar', :baz => 'qux'}
# will GET http://example.com/resource?foo=bar&baz=qux

Request headers can be set by passing a ruby hash containing keys and values representing header names and values:

# GET request with modified headers
RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

Shouldn't we therefore be able to call:

RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}, :params => {:foo => 'bar', :baz => 'qux'}

When I try this I get Error: wrong number of arguments (3 for 1..2)

@ab
Copy link
Member

ab commented Jun 9, 2015

Hi there,

You've discovered a really unfortunate part of the RestClient API. I'm planning to rewrite this in a saner way in RestClient 3.0, but that's a long way off.

In the meantime, this should work:

RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g', :params => {:foo => 'bar', :baz => 'qux'}}

The :params key is actually pulled out of the headers hash.

@ab ab closed this as completed Jun 9, 2015
@kushbhandari
Copy link

Will below code also work (as per https://github.com/rest-client/rest-client) it looks like:

RestClient::Request.execute( { :method => :get, :url => 'http://example.com', :headers => {:params => {:foo => 'bar', :baz => 'qux'}} })

Also, how can I see the request URL which was finally executed (with params etc.), I wanted to confirm that request was actually made to http://example.com?foo=bar&baz=qux ?

@ab
Copy link
Member

ab commented Sep 16, 2015

Yep, that looks right. For testing I usually enable RestClient logging (e.g. RestClient.log = 'stderr') or just look at the resulting server side logs.

@kushbhandari
Copy link

thanks @ab but is there any way to add parameters if the request URL already has parameters something like:

RestClient::Request.execute( { :method => :get, :url => 'http://example.com?param1=value1', :headers => {:params => {:foo => 'bar', :baz => 'qux'}} })

The request URL formed from above is http://example.com?param1=val1?foo=bar&baz=qux which is incorrect.

@cben
Copy link

cben commented Mar 26, 2017

thanks but is there any way to add parameters if the request URL already has parameters

I'm also interested in that. Tried creating a Resource with a :params "header" instead of having it in the URL, but the headers are .merged together:
https://github.com/rest-client/rest-client/blob/5269c842b/lib/restclient/resource.rb#L50
so the inner :params hash replaces the original params:

[4] pry(main)> c = RestClient::Resource.new('httpbin.org/get', headers: {params: {x: 1}})
=> #<RestClient::Resource:0x0055a8eeeb5bf8
 @block=nil,
 @options={:headers=>{:params=>{:x=>1}}},
 @url="httpbin.org/get">

[9] pry(main)>  c.get(params: {y: 2})
RestClient.get "http://httpbin.org/get?y=2", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client/2.0.0 (linux-gnu x86_64) ruby/2.3.1p112"

I can do c.get(params: c.headers[:params].merge(y: 2)) but that feels too tightly coupled to Resource, I'll probably resort to managing my common params on my own...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants