2012-09-08 32 views
8

我目前一直在撞牆,直到我通過這個問題。我使用的是ruby-1.9.3-p194和Rails。我試圖做一個post請求,我可以用net :: HTTP.post_form很好地完成,但是我不能在這裏使用它,因爲我需要在頭文件中設置一個cookie。 http.post錯誤地說RoR Net :: HTTP發佈錯誤undefined方法`bytesize'

"undefined method `bytesize' for #<Hash:0xb1b6c04>" 

因爲我想它試圖對發送的數據執行一些操作。

有沒有人有某種修​​復或變通?

由於

headers = {'Cookie' => 'mycookieinformationinhere'} 
uri = URI.parse("http://asite.com/where/I/want/to/go") 
http = Net::HTTP.new(uri.host, uri.port) 
response = http.post(uri.path, {'test' => 'test'}, headers) 

回答

16

bytesize該方法是在String,不Hash。這是你的第一個線索。第二線索是Net::HTTP#post的文件:向path

柱(路徑,數據,initheader =零,DEST =無)

帖子data(必須是一個字符串)。 header必須是{'Accept'=>'/',...}。

你試圖通過一個Hash,{'test' => 'test'},以post它希望看到一個String。我想你想要更像這樣的東西:

http.post(uri.path, 'test=test', headers) 
+0

啊,謝謝,哈哈。我欺騙了那一個。 – James

+0

http://stackoverflow.com/questions/798710/how-to-turn-a-ruby-hash-into-http-params –