我目前正在反复用头撞墙,直到解决这个问题。我正在使用 ruby-1.9.3-p194 和 Rails。我正在尝试发出一个发布请求,我可以使用 Net::HTTP.post_form 很好地完成该请求,但我不能在这里使用它,因为我需要在标头中设置一个 cookie。 http.post 错误说
"undefined method `bytesize' for #<Hash:0xb1b6c04>"
因为我猜它正在尝试对正在发送的数据执行一些操作。
有人有某种修复或解决方法吗?
Thanks
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)
The bytesize http://ruby-doc.org/core/String.html#method-i-bytesize方法已开启String
, not Hash
。这是你的第一个线索。第二条线索是文档Net::HTTP#post http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html#method-i-post:
post(路径,数据,initheader = nil,dest = nil)
Posts data
(必须是字符串)path
. header
必须是像 { ‘Accept’ => ‘/’, … } 这样的哈希。
你试图传递一个哈希值,{'test' => 'test'}
, to post
它期望看到的地方String
。我想你想要更多这样的东西:
http.post(uri.path, 'test=test', headers)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)