2014-02-19 90 views
0

如何使用RestClient在帖子中發送用戶名和密碼?我的客戶端代碼如下:如何使用RestClient發送用戶名和密碼

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

服務器代碼是在Rails和利用`http_basic_authenticate_with的:名稱=> 「我」,:密碼=> 「12345」。

回答

0

您可以將用戶名和密碼添加到URL字符串,

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

或者你可以在這樣的哈希通過他們,

resource = RestClient::Resource.new('http://localhost:3000/api/rules', :user => 'me', :password => '12345') 
response = resource.post(:books => {:book_name => "Harry Porter", :author => "JR"}) 
相關問題