2

我正在努力从 JSON 中的团队城市 api 获取结果

require 'open-uri'
  url = ".../app/rest/buildQueue/"
  c = Curl::Easy.new(url) do |curl| 
    curl.headers["Content-type"] = "application/json"
    curl.http_auth_types = :basic
    curl.username = 'user'
    curl.password = 'password'
  end
  c.perform
  puts c.body_str

我得到一堆 xml 文本

4

2 回答 2

0

您需要使用Accept标头来控制响应类型:

例如(命令行)

curl --url http://xxx/app/rest/buildQueue/ -H Accept:"application/json"

文档参考

于 2014-10-10T00:20:57.767 回答
0

你也可以使用“net/http”

require 'net/http'
require 'uri'

url = URI('http://localhost:8111/httpAuth/app/rest/agents')
req = Net::HTTP::Get.new(url)
req['Accept'] = 'application/json'
req.basic_auth 'admin', 'admin'

res = Net::HTTP.start(url.hostname, url.port) {|http|
  http.request(req)
}
puts res.body
于 2016-10-08T19:45:23.807 回答