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

Currently, I step my head against the wall several times until I get this problem. I am using ruby-1.9.3-p194 and Rails. I am trying to make a mail request that I can do with Net :: HTTP.post_form, but I can not use it here because I need to set a cookie in the header. http.post mistakenly says

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

because I assume that he is trying to perform some operation on the data being sent.

Does anyone have some kind of fix or work?

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) 
+8
post ruby ruby-on-rails cookies
source share
1 answer

The bytesize method is on String , not Hash . This is your first key. The second key is the documentation for Net::HTTP#post :

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

Messages data (must be a string) before path . header should be a hash, for example {'Accept =>' /, ...}.

You are trying to pass a hash, {'test' => 'test'} , to a post where it expects to see a String . I think you need something more:

 http.post(uri.path, 'test=test', headers) 
+16
source

All Articles