如何使用RestClient发送用户名和密码

时间:2014-02-19 07:27:03

标签: ruby-on-rails ruby-on-rails-4 rest-client

如何使用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:name => “我”,:密码=> “12345”。

1 个答案:

答案 0 :(得分: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"})