9

我目前正在反复用头撞墙,直到我通过这个问题。我正在使用 ruby​​-1.9.3-p194 和 Rails。我正在尝试发出一个可以使用 Net::HTTP.post_form 完成的发布请求,但我不能在这里使用它,因为我需要在标题中设置一个 cookie。http.post 出错说

"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)
4

1 回答 1

18

bytesize方法是开,String不是Hash。这是你的第一个线索。第二个线索是文档Net::HTTP#post

发布(路径,数据,initheader = nil,dest = nil)

帖子data(必须是字符串)到path. header必须是像 { 'Accept' => '/', ... } 这样的 Hash。

您正在尝试将 Hash, {'test' => 'test'}, 传递到post它期望看到String. 我想你想要更像这样的东西:

http.post(uri.path, 'test=test', headers)
于 2012-09-08T00:33:36.273 回答