undefined byte_size for hash - ruby

Im trying to get around a get request in ruby.
uri = URI.parse(ENV["DATA_URL"])
http = Net::HTTP.new(uri.host, uri.port)
headers["Authorization"] = data["authHeader"]
request = Net::HTTP::Get.new(uri.request_uri,headers)
response = http.request( request, form_data )
JSON.parse(response.body)
Im not sure why this is happening, any help is appreciated

Your form_data is probably a Hash and it needs to be formatted into a string. Try:
formatted_data = URI.encode_www_form(form_data)
response = http.request( request, formatted_data )
http://docs.ruby-lang.org/en/trunk/URI.html#method-c-encode_www_form

Issue could be because of form_data attribute. I use URI.encode_www_form when passing the data. You can refer here for URI::encode_www_form
You could do URI.encode_www_form( form_data ) and fix the issue

Related

Got the error File type is not supported when uploading a file in ruby on rails

url = URI("https://api.podium.com/v4/messages/attachment")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "multipart/form-data"
request["Authorization"] = "Bearer #{access_token}"
form_data = [["attachment",File.open('D:\proj\v5\ap\fl\Screenshot (1).png')],['data', "#{request_data}"]]
request.set_form(form_data, 'multipart/form-data')
response = https.request(request)
response_body = JSON.parse(response.body)
if response.code == '200' || response.code == '201'
return response_body,'success'
else
return response_body,"#{response.message}"
end
rescue Exception => ex
return ex,'Exception'
end
**
When i am sending the request i got the error like
{"code"=>"invalid_request_values", "message"=>"File type is not supported.", "moreInfo"=>"https://docs.podium.com/docs/errors#invalid_request_values"}
**
Here are a couple of things you could try:
The podium documentation says that the images cannot be above 5mb in size. You can verify if this is the case.
https://help.podium.com/hc/en-us/articles/360039896873-Sending-Messages#Attach%20media%20to%20a%20message
I noticed the code snippet you've shared does set have this line as mentioned in their documentation here https://docs.podium.com/reference/messagesend_with_attachment
request["accept"] = 'application/json'
Maybe adding this header might fix it for you, as you are saying that it is working for you in Postman but not in Ruby.
Try uploading the file from the API Doc reference page itself and check out the code sample they provide there. There are some differences in the code sample you've shared, and the one that podium shows in their doc.

Ruby putting token key in requests

I dont know how to put my key into my requests so they are sent back as
{"status"=>"400", "message"=>"Token parameter is required."}
This is the code I have been using
require 'net/http'
require 'json'
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
uri = URI(url)
response = Net::HTTP.get(uri)
response.authorization = token
puts JSON.parse(response)
I have tried a couple different things I've found on the internet but all of them just give errors for
undefined method `methodname' for #<String:0x00007fd97519abd0>
According to the API documentation (based on the URL you referenced), you need to provide the token in a header named token.
So, you should probably try some variation of the below (code untested):
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
uri = URI(url)
request = Net::HTTP::Get.new(uri)
request['token'] = token
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
More information about Net:HTTP headers can be found in this StackOverflow answer.
As a side note, if you are not locked on using Net::HTTP, consider switching to a friendlier HTTP client, perhaps HTTParty. Then, the complete code looks like this:
require 'httparty'
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
response = HTTParty.get url, headers: { token: token }
puts response.body

URI.parse doesn't want to parse uri with | (pipe) char

I'm trying to send get request with:
require "net/https"
require "uri"
...
uri = "https://graph.facebook.com/v2.2/#{event_id}?access_token=#{access_token}"
uri = URI.parse(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body
Facebook sent me an acces_token with | char, and because of it uri = URI.parse(uri) throws an error: URI::InvalidURIError: bad URI(is not URI?) https:/ ....
Is there any other parser, or should I manually extract these host, port, request_uri values? What's the best way of fixing it?
access_token looks like 141112112181111|9Alz3xW7P0CQ_DRomJN2oMeiwXs
event_id is for example 385101904985590
edit: just realized that it's structure is APP_ID|some_token
Have you tried encoding the URI with URI#encode?
uri = 'http://foobar.com?foo=something|weird'
uri = URI.encode(uri)
uri = URI.parse(uri) => #<URI::HTTP:0x007fe2f48775b0 URL:http://foobar.com?foo=something%7Cweird>
Don't try to inject variables into URI query parameters, especially tokens and keys and such as that's a good route to creating an invalid one. Instead, rely on the URI class and let it handle as much as possible.
require 'uri'
event_id = 1
access_token = 'foo|bar'
uri = URI.parse("https://graph.facebook.com/v2.2/#{event_id}")
uri.query = URI.encode_www_form(['access_token', access_token])
uri.to_s # => "https://graph.facebook.com/v2.2/1?access_token&foo%7Cbar"
If you want something more full-featured, Addressable::URI is very nice.
See "Parsing string to add to URL-encoded URL" for more information.

View request text before sending

I'm using Net::HTTP to make a POST request:
uri = 'service.example.com'
https = Net::HTTP.new(uri)
https.use_ssl = true
path = "/service_action"
data = request_body_obj.to_json
response = https.post(path, data, {'Content-Type' => 'application/json'})
My request is timing out for some reason. To debug why, I'd like to see the text of the request body and headers of my request. Is there a way to do that?

Sending http post request in Ruby by Net::HTTP

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
Try this:
For detailed documentation, take a look at:
http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
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
request = Net::HTTP::Post.new(uri.path)
request['HEADER1'] = 'VALUE1'
request['HEADER2'] = 'VALUE2'
response = https.request(request)
puts response
The second argument of Net::HTTP#post needs to be a String containing the data to post (often form data), the headers would be in the optional third argument.
As qqx mentioned, the second argument of Net::HTTP#post needs to be a String
Luckily there's a neat function that converts a hash into the required string:
response = https.post(uri.path, URI.encode_www_form(headers))

Resources