我有以下方法:
http_client=Net::HTTP.new(@uri.host,@uri.port)
request=Net::HTTP::Post.new(@uri.request_uri)
request.set_form_data(params)
request.basic_auth(@user,@pass)
result = http_client.request(request)
#
# !!! .content_type does not exist !!!
#
result=case result.content_type
when "application/json" JSON.parse(result,{:symbolize_names => true})
when "application/xml" XMLHELPER.parse(result)
end
result
那么如何确定服务器发送的内容类型呢?
答案 0 :(得分:3)
您在致电NoMethodError
时收到result.content_type
,因为您已将result
分配给响应正文,这是一个字符串。
[...]
response = client.request(request)
result = case response.content_type
when "application/json" JSON.parse(response.body, {:symbolize_names => true})
when "application/xml" XMLHELPER.parse(response.body)
end