I'm sending a request with custom headers to a web service.
require 'uri'
require 'net/http'
uri = URI("https://api.site.com/api.dll")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
headers =
{
'HEADER1' => "VALUE1",
'HEADER2' => "HEADER2"
}
response = https.post(uri.path, headers)
puts response
It's not working, I'm receiving an error of:
/usr/lib/ruby/1.9.1/net/http.rb:1932:in `send_request_with_body': undefined method `bytesize' for #<Hash:0x00000001b93a10> (NoMethodError)
How do I solve this?
P.S. Ruby 1.9.3
As qqx mentioned, the second argument of
Net::HTTP#post
needs to be aString
Luckily there's a neat function that converts a hash into the required string:
The second argument of
Net::HTTP#post
needs to be aString
containing the data to post (often form data), the headers would be in the optional third argument.Try this:
For detailed documentation, take a look at: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html