9

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)
mu is too short
  • 426,620
  • 70
  • 833
  • 800
James
  • 680
  • 2
  • 8
  • 22

1 Answers1

18

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)
mu is too short
  • 426,620
  • 70
  • 833
  • 800