Ruby HTTP Post Request - 内容类型?

时间:2011-07-13 09:16:38

标签: ruby content-type httprequest

我有以下方法:

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

那么如何确定服务器发送的内容类型呢?

1 个答案:

答案 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