I am BARRY HESS > Blog

ActiveResource HTTP Basic Auth With Email Address

As I’ve started to play around here and there with ActiveResource, I’ve noticed lots of complaints about how ActiveResource’s implementation of HTTP basic auth leaves a lot to be desired. Specifically with regard to its lack of support for email addresses as usernames. This won’t work:

class Expense < ActiveResource::Base
  self.site = "https://barry@bjhess.com:password@website.com"
end

Turns out it’s fairly simple to authorize via email address in ActiveResource. Just send the header:

class Expense < ActiveResource::Base
  self.site = "https://website.com"
  @headers  = { 'Authorization' => "Basic (#{Base64.encode64('barry@bjhess.com:password').strip})" }
end