Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR Net::HTTP post error undefined method `bytesize'

I am currently banging my head against the wall repeatedly until I get passed this issue. I'm using ruby-1.9.3-p194 and Rails. I'm attempting to make a post request which I can do fine with Net::HTTP.post_form, but I can't use that here because I need to set a cookie in the header. http.post is erroring saying

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

because I guess it's trying to perform some operation on the data being sent.

Does anyone have some kind of fix or work around?

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)
like image 475
James Avatar asked Sep 08 '12 00:09

James


1 Answers

The bytesize method is on String, not Hash. That's your first clue. The second clue is the documentation for Net::HTTP#post:

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

Posts data (must be a String) to path. header must be a Hash like { ‘Accept’ => ‘/’, … }.

You're trying to pass a Hash, {'test' => 'test'}, to post where it expects to see a String. I think you want something more like this:

http.post(uri.path, 'test=test', headers)
like image 62
mu is too short Avatar answered Nov 15 '22 06:11

mu is too short