我目前正在尝试在HTTParty中发送一个登录并获取一些API数据的get请求。我通过以下方式成功完成了这项工作:
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\"" })
任何提示都将不胜感激!
答案 0 :(得分:0)
我发现curl中的-u USERNAME:PASSWORD
大部分相当于HTTParty中的basic_auth
。我还喜欢在一个单独的变量中设置我的选项,使其更容易阅读/编辑/调整,然后将该变量传递给调用。
options = {
basic_auth: { username: USERNAME, password: PASSWORD }
}
HTTParty.post(<URL>, options)