【问题标题】:RoR Net::HTTP post error undefined method `bytesize'RoR Net::HTTP 发布错误未定义方法“字节大小”
【发布时间】:2012-09-01 20:35:15
【问题描述】:

我目前正在反复用头撞墙,直到我通过这个问题。我正在使用 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)

【问题讨论】:

    标签: ruby-on-rails ruby post cookies


    【解决方案1】:

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

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

    data(必须是字符串)发布到pathheader 必须是像 { ‘Accept’ => ‘/’, ... } 这样的哈希。

    您正在尝试将哈希 {'test' =&gt; 'test'} 传递给 post,它希望看到 String。我想你想要更多这样的东西:

    http.post(uri.path, 'test=test', headers)
    
    猜你喜欢
    • 1970-01-01
    • 2014-01-30
    • 2017-11-12
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2013-06-02
    • 2019-06-23
    • 2015-06-28
    相关资源
    最近更新 更多