【问题标题】:URI.parse doesn't want to parse uri with | (pipe) charURI.parse 不想用 | 解析 uri (管道)字符
【发布时间】:2014-12-10 20:36:23
【问题描述】:

我正在尝试发送获取请求:

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 给我发送了一个带有| 字符的acces_token,因此uri = URI.parse(uri) 抛出一个错误:URI::InvalidURIError: bad URI(is not URI?) https:/ ....

是否有任何其他解析器,或者我应该手动提取这些主机、端口、request_uri 值?修复它的最佳方法是什么?

access_token 看起来像 141112112181111|9Alz3xW7P0CQ_DRomJN2oMeiwXs

event_id 是例如385101904985590

编辑:刚刚意识到它的结构是APP_ID|some_token

【问题讨论】:

  • 您能否发布access_tokenevent_id 内容的匿名版本,以便我们全面了解您的内容?您可能需要编码access_token
  • 解析器拒绝这个 URI 是正确的,切换到另一个解析器也无济于事:| 在 URI 中是非法的。期间。

标签: ruby


【解决方案1】:

您是否尝试使用 URI#encode 对 URI 进行编码?

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>

【讨论】:

    【解决方案2】:

    不要尝试将变量注入 URI 查询参数,尤其是令牌和键等,这是创建无效参数的好方法。相反,依赖 URI 类并让它尽可能地处理。

    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"
    

    如果您想要功能更全面的东西,Addressable::URI 非常好。

    有关详细信息,请参阅“Parsing string to add to URL-encoded URL”。

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      相关资源
      最近更新 更多