gorootde
gorootde

Reputation: 4073

Ruby HTTP Post Request - Content Type?

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?

Upvotes: 3

Views: 1617

Answers (1)

Johannes Gorset
Johannes Gorset

Reputation: 8785

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

Upvotes: 3

Related Questions