How do I do basic authentication with RestClient?

Here is an example of working code where I support optional basicauth but don't require the user and password be embedded in the URL:

def get_collection(path)
  response = RestClient::Request.new(
    :method => :get,
    :url => "#{@my_url}/#{path}",
    :user => @my_user,
    :password => @my_pass,
    :headers => { :accept => :json, :content_type => :json }
  ).execute
  results = JSON.parse(response.to_str)
end

Do note if @my_user and @mypass are not instantiated, it works fine without basicauth.


The easiest way is to embed the details in the URL:

RestClient.get "http://username:[email protected]"