Crusader633 Crusader633 - 1 year ago 174
Ruby Question

Ruby undefined method `bytesize' for #<Hash:0x2954fe8>

i have the following Ruby Code, for a tracking website in sandbox mode:

require "net/http"
require "net/https"
require "uri"

xml = <<XML
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><data appname="dhl_entwicklerportal" language-code="de" password="Dhl_123!" request="get-status-for-public-user"><data piece-code="00340433836536550280"></data></data>

uri = URI('')

nhttp =, uri.port)
request =
request.basic_auth 'xpackageWP', 'hidden'
response = nhttp.start {|http|
http.request(request, xml:xml)

puts response.body

I always get the error :

d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body' undefined method `bytesize' for #<Hash:0x2954fe8> (NoMethodError)
from d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:130:in `exec'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1404:in `block in transport_request'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from D:/Dropbox_5BHIF/Dropbox/TempDHL/Main.rb:17:in `block in <main>'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:852:in `start'
from D:/Dropbox_5BHIF/Dropbox/TempDHL/Main.rb:16:in `<main>'

I tried really hard to solve this, but i cannot think of any problem.
When i test it in the Browser with the Link and then a ?xml= it works perfectly, so it seems to be a problem with my Ruby Code.

Answer Source

You're sending post data as a hash. You should encode it as string.

For example Using URI::encode_www_form:

request =
response = nhttp.start do |http|
  post_data = URI.encode_www_form({xml: xml})
  http.request(request, post_data)

UPDATE If you want GET request, append the query string to the url.

post_data = URI.encode_www_form({xml: xml})
uri = URI('' +


response = nhttp.start do |http|
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download