require 'net/http'
require 'rubygems'
require 'json'
url = URI.parse('http://www.xyxx/abc/pqr')
resp = Net::HTTP.get_response(url) # get_response takes an URI object
data = resp.body
puts data
这是我在 ruby 中的代码,resp.data 以 xml 形式给我数据。
rest api 默认返回 xml 数据,如果 header content-type 是 application/json 则返回 json。
但我想要 json 格式的数据。为此我必须设置 header['content-type']='application/json'。
但我不知道,如何使用 get_response 方法设置标头。获取 json 数据。