【问题标题】:HTTParty - how to generate authorisation id from username and passwordHTTParty - 如何从用户名和密码生成授权 id
【发布时间】:2014-07-16 15:51:10
【问题描述】:

我在命令行上使用以下 curl 命令生成令牌 ID,然后在 HTTParty 代码中使用它。

curl -d "username=Yoshi.Nosaka@test.com&password=testpwd" http://upload.test.org:8026/api2/auth-token/

上面的curl命令生成token id:

6cb2997265877d4cee5467e93577fa

然后我在我的 rails 应用程序的 HTTParty 命令中使用令牌 ID。示例:

HTTParty.get("http://upload.test.org:8026/api2/repos/", :headers => { "Authorization" => "Token 6cb2997265877d4cee5467e93577fa"}) 

我想知道如何在我的 rails 应用程序中使用 HTTParty 生成该令牌 ID,而不是在命令行上生成。

非常感谢您的所有建议。

【问题讨论】:

    标签: ruby-on-rails curl ruby-on-rails-3.2 httparty


    【解决方案1】:

    cURL -d 选项在 POST 上发送数据。

    curl -d "username=Yoshi.Nosaka@test.com&password=testpwd" http://upload.test.org:8026/api2/auth-token/
    

    我不知道您的应用是否处理 json 请求。但如果是这样,您可以使用类似上述的方法。

    HTTParty.post("http://upload.test.org:8026/api2/auth-token/",
      { 
        :body => [ { "username" => "Yoshi.Nosaka@test.com", "password" => "testpwd" } ].to_json,
        :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
      })
    

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      相关资源
      最近更新 更多