8

I have already created a basic authentication key, now I am just trying to utilize it. I have tried a few different variations, but none seem to show Authorization in the request headers.

$auth = 'Basic cmFtZXNoQHVzYW1hLmNvbTpyYW1lc2h1JEBtcA=='


@response = resource.post('Authorization' => $auth)
nor
@response = resource.post(:authorization => $auth)
nor
@response = resource.post(:Authorization => $auth)
nor
@response = resource.post(:content_type => :json, :accept => :json, :headers => { 'Authorization:' => $auth })

Unfortunately I am not finding a lot of info in the rdoc that can help me solve this. Does anyone have experience adding auth headers using the Rest Client gem?

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
Josh
  • 460
  • 1
  • 5
  • 20

4 Answers4

16

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.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
  • 5
    `Base64.encode64` inserts newlines if the username and password are very long. To avoid that, you can either add `.gsub(/\n/, '')` or use `Base64.strict_encode64`. – narced133 Feb 26 '15 at 18:33
3

Even though I didn't have a payload to send I was trying to send one without. This ended up being the cause. So I included:

json_str = ''
@response = resource.post(json_str, :content_type => :json, :accept => :json, :Authorization => $auth)

And this worked.

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
Josh
  • 460
  • 1
  • 5
  • 20
2

If you don't want to use RestClient::Resource, you can include basic auth in a request like this:

RestClient::Request.execute method: :get, url: url, user: 'username', password: 'secret'

The trick is not to use the RestClient.get (or .post, .put etc.) methods since all options you pass in there are used as headers.

Manuel Meurer
  • 3,238
  • 6
  • 35
  • 50
-1

This worked great for me, in case anyone wants to use username/password

RestClient.post("https://USERNAME:PASSWORD@yoursite.com/something", { some: "payload data" })