How to URL encode a string in Ruby

后端 未结 8 802
醉梦人生
醉梦人生 2020-12-02 05:34

How do I URI::encode a string like:

\\x12\\x34\\x56\\x78\\x9a\\xbc\\xde\\xf1\\x23\\x45\\x67\\x89\\xab\\xcd\\xef\\x12\\x34\\x56\\x78\\x9a
         


        
相关标签:
8条回答
  • 2020-12-02 06:03

    I created a gem to make URI encoding stuff cleaner to use in your code. It takes care of binary encoding for you.

    Run gem install uri-handler, then use:

    require 'uri-handler'
    
    str = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a".to_uri
    # => "%124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A"
    

    It adds the URI conversion functionality into the String class. You can also pass it an argument with the optional encoding string you would like to use. By default it sets to encoding 'binary' if the straight UTF-8 encoding fails.

    0 讨论(0)
  • 2020-12-02 06:08
    str = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a".force_encoding('ASCII-8BIT')
    puts CGI.escape str
    
    
    => "%124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A"
    
    0 讨论(0)
  • 2020-12-02 06:08

    Code:

    str = "http://localhost/with spaces and spaces"
    encoded = URI::encode(str)
    puts encoded
    

    Result:

    http://localhost/with%20spaces%20and%20spaces
    
    0 讨论(0)
  • 2020-12-02 06:13

    If you want to "encode" a full URL without having to think about manually splitting it into its different parts, I found the following worked in the same way that I used to use URI.encode:

    URI.parse(my_url).to_s
    
    0 讨论(0)
  • 2020-12-02 06:20

    You can use Addressable::URI gem for that:

    require 'addressable/uri'   
    string = '\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a'
    Addressable::URI.encode_component(string, Addressable::URI::CharacterClasses::QUERY)
    # "%5Cx12%5Cx34%5Cx56%5Cx78%5Cx9a%5Cxbc%5Cxde%5Cxf1%5Cx23%5Cx45%5Cx67%5Cx89%5Cxab%5Cxcd%5Cxef%5Cx12%5Cx34%5Cx56%5Cx78%5Cx9a" 
    

    It uses more modern format, than CGI.escape, for example, it properly encodes space as %20 and not as + sign, you can read more in "The application/x-www-form-urlencoded type" on Wikipedia.

    2.1.2 :008 > CGI.escape('Hello, this is me')
     => "Hello%2C+this+is+me" 
    2.1.2 :009 > Addressable::URI.encode_component('Hello, this is me', Addressable::URI::CharacterClasses::QUERY)
     => "Hello,%20this%20is%20me" 
    
    0 讨论(0)
  • 2020-12-02 06:23

    Nowadays, you should use ERB::Util.url_encode or CGI.escape. The primary difference between them is their handling of spaces:

    >> ERB::Util.url_encode("foo/bar? baz&")
    => "foo%2Fbar%3F%20baz%26"
    
    >> CGI.escape("foo/bar? baz&")
    => "foo%2Fbar%3F+baz%26"
    

    CGI.escape follows the CGI/HTML forms spec and gives you an application/x-www-form-urlencoded string, which requires spaces be escaped to +, whereas ERB::Util.url_encode follows RFC 3986, which requires them to be encoded as %20.

    See "What's the difference between URI.escape and CGI.escape?" for more discussion.

    0 讨论(0)
提交回复
热议问题