1

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

Marcin Doliwa
  • 3,639
  • 3
  • 37
  • 62
  • Can you post an anonymized version of the contents of `access_token`, also `event_id` so we can get a full sense of what you have there? You may need to encode `access_token`. – Michael Berkowski Dec 10 '14 at 20:39
  • 1
    The parser is correct in rejecting this URI, and switching to another parser won't help: `|` is illegal in a URI. Period. – Jörg W Mittag Dec 10 '14 at 21:09

2 Answers2

5

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>
pdoherty926
  • 9,895
  • 4
  • 37
  • 68
0

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.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303