RoR Net :: HTTP post error undefined方法`bytesize'

Jam*_*mes 8 ruby cookies post ruby-on-rails

我正在反复撞墙,直到我通过这个问题.我正在使用ruby-1.9.3-p194和Rails.我正在尝试发布一个帖子请求,我可以用Net :: HTTP.post_form做得很好,但我不能在这里使用它,因为我需要在标题中设置一个cookie.http.post说错了

"undefined method `bytesize' for #<Hash:0xb1b6c04>"
Run Code Online (Sandbox Code Playgroud)

因为我猜它正试图对正在发送的数据执行一些操作.

有没有人有某种修​​复或解决方法?

谢谢

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)
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 16

bytesize方法是String,不Hash.那是你的第一个线索.第二条线索是以下文件Net::HTTP#post:

post(path,data,initheader = nil,dest = nil)

帖子data(必须是一个字符串)来path.header必须是像''Accept'=>'/',...}这样的哈希.

你正试图将哈希传递{'test' => 'test'}post它希望看到的地方String.我想你想要更像这样的东西:

http.post(uri.path, 'test=test', headers)
Run Code Online (Sandbox Code Playgroud)