Pergunta

How do I send the username and password in a post using RestClient? My client code is below:

response = RestClient.post 'http://localhost:3000/api/rules',
    :name => "me", :password => "12345",
    :books => {:book_name => "Harry Porter",
           :author => "JR"}

The server code is in Rails and makes use of `http_basic_authenticate_with :name => "me", :password => "12345".

Foi útil?

Solução

You can add the username and password to the URL string,

username = 'me'
password = '12345'
response = RestClient.post "http://#{username}:#{password}@localhost:3000/api/rules",
  :books => {:book_name => "Harry Porter", :author => "JR"}

Or you can pass them in a hash like this,

resource = RestClient::Resource.new('http://localhost:3000/api/rules', :user => 'me', :password => '12345')
response = resource.post(:books => {:book_name => "Harry Porter", :author => "JR"})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top