Ruby undefined method `bytesize' for # Hash:0x2954fe8
You're sending post data as a hash. You should encode it as string.
For example Using URI::encode_www_form
:
request = Net::HTTP::Post.new(uri)
...
response = nhttp.start do |http|
post_data = URI.encode_www_form({xml: xml})
http.request(request, post_data)
end
UPDATE If you want GET request, append the query string to the url.
post_data = URI.encode_www_form({xml: xml})
uri = URI('https://cig.dhl.de/services/sandbox/rest/sendungsverfolgung?' +
post_data)
...
response = nhttp.start do |http|
http.request(request)
end
Undefined method `bytesize' for # Hash
I think your having the same problem that's been discussed here: error happens when I try "all" method in datamapper
In your case, Sinatra is trying to take the return value of @lavels.update and turn that into a string to display to the user.
Try this to see if it fixes the problem:
get '/job' do
@labels = Labels.new
@labels.update
"Labels Updated"
end
Your return value is now a string, so you shouldn't get the error.
undefined method `bytesize' for { COUNT(*) = 0}:Hash
Your code is trying to return the hash results
after the function ends. Return with a proper response code/type and it should work.
Last line/block in Ruby is returned in any function. In your case, last block is results.each
which returns back results
which is not a valid response type for sinatra.
rails no bitesize error to 3rd party API response
From Pardeep's link, try
result = client.execute(
api_method: service.freebusy.query,
body: URI.encode_www_form({ timeMin: '2015-12-24T17:06:02.000Z',
timeMax: '2016-01-30T17:06:02.000Z',
items: [{ id: social_object.email }]}),
headers: {'Content-Type' => 'application/json'})
The problem is that the body could not be sent as a hash. You should encode as an string.
Related Topics
Getting the Full Rspec Test Name from Within a Before(:Each) Block
Ruby: How to Find Non-Unique Elements in Array and Print Each with Number of Occurrences
Gem::Ext::Builderror: Error: Failed to Build Gem Native Extension Bcrypt-Ruby
How to Configure Action Mailer (Should I Register Domain)
Why Does Openuri Treat Files Under 10Kb in Size as Stringio
How to Replace the Last Occurrence of a Substring in Ruby
In Ruby, How to Read Data Column Wise from a CSV File
Sinatra - Response.Set_Cookie Doesn't Work
How Does MACports Install Packages? How to Activate a Ruby Installation Done via MACports
Why Is Date.Today - 6.Months + 6.Months != Date.Today
Nokogiri To_Xml Without Carriage Returns
Ruby on Rails: If Current Page? Is Homepage, Don't Show Form