Search code examples
rubyrest-client

Missing parameter client_id?? Ruby RestClient issues


I'm using RestClient to work with the Spotify API (but I'm assuming this is a general issue, not specifically spotify).

The API requires passing client_id. My code: authorization = Base64.strict_encode64 "#{@clientID}:#{@clientSecret}" begin

request_body = { response_type: 'code', client_id: @clientID, redirect_uri: REDIRECT_URI }
#response = RestClient.get(AUTHORIZE_URI, request_body)
puts response = RestClient::Request.execute(method: :get, url: AUTHORIZE_URI, payload: request_body)

rescue RestClient::BadRequest => e puts e.response end

But I'm getting a BadRequest exception, and the response states "Missing required parameter: client_id".

If I do a curl:

puts `curl -I -s -X GET "#{AUTHORIZE_URI}?client_id=#{@clientID}&response_type=code&redirect_uri=REDIRECT_URI"`

I get a normal, 200 OK response. What is going on here????


Solution

  • you have to use params key inside headers hash to pass query parameters. Documentation below

    Due to unfortunate choices in the original API, the params used to populate the query string are actually taken out of the headers hash. So if you want to pass both the params hash and more complex options, use the special key :params in the headers hash. This design may change in a future major release.

    RestClient::Request.execute(method: :get, 
                                url: 'http://example.com/resource',
                                timeout: 10, 
                                headers: {params: {foo: 'bar'}}
                               )
    
     ➔ GET http://example.com/resource?foo=bar