Search code examples
rubybasic-authenticationrest-client

RestClient basic auth with # in username


I have a problem with a daemon accessing a REST api.

The access requires basic authentication. The username and password are fixed and can not be changed.

The problem seems to be, that the username looks like this: #ws+R4nd0mS7r1n

I access the API like this:

resource = RestClient::Resource.new( "#{base_url}/failover/#{failover_ip}", { :user => user_name, :password => user_password})
response = resource.get

This gets me an bad URI error:

bad URI(absolute but no path): https://#ws+R4nd0mS7r1n:RaNdOmPaSsWoRd@robot-ws.your-server.de/failover/11.11.11.11

When I itentionally remove the # from the username it works, but I get a NOT Authenticated error.

Is there a way to pass a username or password containing # to restclient? Passing the complete URI manually to a .get does not work either.


Solution

  • 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