How to Do Basic Authentication with Restclient

How do I do basic authentication with RestClient?

From the source it looks like you can just specify user and password as part of your request object.

Have you tried something like:

r = Request.new({:user => "username", :password => "password"})

Also if you look down in the Shell section of the ReadMe it has an example of specifying it as part of restshell.

$ restclient https://example.com user pass
>> delete '/private/resource'

How do I do Rails Basic Authorization with RestClient?

Okay... so first it helps if I go to the right URL:

authentication_url = @alm_url + "rest/is-authenticate"

Which should read:

authentication_url = @alm_url + "authentication-point/authenticate"

Secondly, it helps if I read the docs for RestClient rather than just look at the readme. The example under Instance Method Details helped a lot.

My code now looks like:

@alm_url       = "http://alm_url/qcbin/"
@user_name = "username"
@user_password = "password"

authentication_url = @alm_url + "authentication-point/authenticate"
resource = RestClient::Resource.new(authentication_url, @user_name, @user_password)
response = resource.get

Rails.logger.debug response.inspect

EDIT:

Wow I really over-thought this. I could have gone with:

response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate"

RestClient basic auth with # in username

I don't get the same error. What version of rest-client do you have installed?

You may simply be able to update the version to fix your problem (I tested with version 1.6.7 of the gem)

Alternatively, this works around the URI failure by directly writing to the Authorization header (which is where this data ends up anyway):

require 'base64'
auth = 'Basic ' + Base64.encode64( "#{user_name}:#{user_password}" ).chomp
resource = RestClient::Resource.new( "#{base_url}/failover/#{failover_ip}", { :headers => { 'Authorization' => auth } } )
resource.get

Groovy rest client without libraries with basic authentication

https://en.wikipedia.org/wiki/Basic_access_authentication

In basic HTTP authentication, a request contains a header field in the form of

Authorization: Basic <credentials>

where <credentials> is the Base64 encoding of ID and password joined by a single colon :.

in groovy it could look like this:

def user = ...
def pass = ...
def auth = "${user}:${pass}".bytes.encodeBase64()
connection.setRequestProperty("Authorization", "Basic ${auth}")

Authentication headers using Rest Client Ruby Gem

For Basic Auth, you should be able to set the user and password in plaintext when you create the resource:

resource = RestClient::Resource.new( 'http://example.com', 'user', 'password' )

But if you really need to set the header directly per request:

@response = resource.post( request_payload, :Authorization => $auth )

should work. If it does not, then you may have set $auth incorrectly. However, I think you just missed adding the request payload, so it was using the hash you supplied for that required param, and not setting any headers at all.

Here's a complete and working example using get (I don't have a test service available with Basic Auth and POST)

require 'rest-client'
require 'base64'
$auth = 'Basic ' + Base64.encode64( 'user:passwd' ).chomp
$url = 'http://httpbin.org/basic-auth/user/passwd'

@resource = RestClient::Resource.new( $url )
@response = @resource.get( :Authorization => $auth )
# => "{\n \"authenticated\": true,\n \"user\": \"user\"\n}"

Note: Though this works, I recommend you use the first and simplest method of supplying user and password to the constructor unless you have good reason not to.



Related Topics



Leave a reply



Submit