【问题标题】:Convert curl command to HTTParty Rails将 curl 命令转换为 HTTParty Rails
【发布时间】:2015-08-06 16:22:00
【问题描述】:

我目前正在尝试在 HTTParty 中发送一个获取请求,该请求会登录并获取一些 API 数据。我通过 curl 成功地做到了这一点:

curl -v --user USERNAMEHERE:PassWordHere http://app.ionitnetworks.com/ionit-app/api/rest/v1/devices

这在我的控制台中输出:

➜  HeatWatch git:(kunzig_new_heatwatch) ✗ curl -v --user (userOmitted):(password_omitted) http://app.ionitnetworks.com/ionit-app/api/rest/v1/devices
* Hostname was NOT found in DNS cache
*   Trying 23.92.23.58...
* Connected to app.ionitnetworks.com (23.92.23.58) port 80 (#0)
* Server auth using Basic with user 'userOmitted'
> GET /ionit-app/api/rest/v1/devices HTTP/1.1
> Authorization: Basic ZG91Z0dofW301320Y2AsayZlLmNvbTpkb3VnMTIz
> User-Agent: curl/7.35.0
> Host: app.ionitnetworks.com
> Accept: */*

我已经尝试了很多东西来让它在 httparty 中工作,但没有任何工作,例如:

@response = HTTParty.post("https://app.ionitnetworks.com/ionit-app/api/rest/v1/devices.json", :headers =>{ "Authorization" =>"Token token=\"AccountsTokenOmitted\"" })

任何提示将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    我发现 curl 中的 -u USERNAME:PASSWORD 在很大程度上等同于 HTTParty 中的 basic_auth。我还喜欢将我的选项设置在一个单独的变量中,以便更容易阅读/编辑/调整,然后将该变量传递给调用。

    options = {
      basic_auth: { username: USERNAME, password: PASSWORD }
    }
    
    HTTParty.post(<URL>, options)
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      相关资源
      最近更新 更多