2014-02-19 53 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"}) 
相关问题