【发布时间】:2013-04-07 04:49:56
【问题描述】:
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 数据。
【问题讨论】:
-
感谢您的编辑,并使其更加清晰。 @rubylovely