Ruby Undefined Method 'Bytesize' for #<Hash:0X2954Fe8>

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



Leave a reply



Submit