2011-07-13 33 views
3

私は、次の方法があります:サーバーによって送信されたコンテンツタイプを決定するためにどのようにRuby HTTP投稿要求 - コンテンツタイプ?

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 

を?

答えて

3

応答ボディにresultが割り当てられているため、NoMethodErrorresult.content_typeにコールされています。これは文字列です。

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

申し訳ありません。それは私のせいだった。 Iv'eはそれを編集ボックスにプログラミングしました - 次回はコピーしますnペーストしてください;) - >修正されたバージョンは最初の投稿を参照してください。 – gorootde

+0

'result.content_type'が' nil'の場合、サーバは送信しませんでした。 –

関連する問題