简体   繁体   中英

How to send username and password using RestClient

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".

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"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM