【问题标题】:How to send username and password using RestClient如何使用 RestClient 发送用户名和密码
【发布时间】:2014-02-19 16:04:29
【问题描述】:

如何使用 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 => "me", :password => "12345"。

【问题讨论】:

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


    【解决方案1】:

    您可以将用户名和密码添加到 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"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 2017-04-19
      • 2010-12-05
      • 2014-07-12
      • 2014-03-03
      相关资源
      最近更新 更多