开发者

Ruby HTTP Post Request - Content Type?

开发者 https://www.devze.com 2023-03-19 18:09 出处:网络
I have the following Method: http_client=Net::HTTP.new(@uri.host,@uri.port) request=Net::HTTP::Post.new(@uri.request_uri)

I have the following Method:

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

So how to determine the content-type that was开发者_开发知识库 sent by the server?


You're getting a NoMethodError from calling result.content_type because you've assigned result to the response body, which is a string.

[...]
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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号