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)

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)

Need Your Help

Recording website load times?

javascript unit-testing firefox testing firebug

I am trying to record the time it takes to load my website on an average (say 10 runs) from various locations in the world. For this, I was thinking of using a list of proxies to achieve this and a...

How can i load a link in other browser from web view?

android webview

I am loading string value in my web view and i need when i click on the link shown in web view should open in other web browser of phone.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.