JVK*_*JVK 3 ruby url urlencode url-encoding
我有一个URL字符串,其中有换行符和回车符.例如
http://xyz.com/hello?name=john&msg=hello\nJohn\n\rgoodmorning¬e=last\night I went to \roger
Run Code Online (Sandbox Code Playgroud)
我的实际msg字符串是:
hello
john
goodmorning
Run Code Online (Sandbox Code Playgroud)
和note字符串是
last\night I went to \roger
Run Code Online (Sandbox Code Playgroud)
为了正确发送它我必须urlencode这个
http://xyz.com/hello?name%3Djohn%26msg%3Dhello%5CnJohn%5Cn%5Crgoodmorning%26note%3Dlast%5Cnight%20I%20went%20to%20%5Croger
Run Code Online (Sandbox Code Playgroud)
但这个编码搞乱\n和\ r.虽然我希望\n应该转换为%0A和\ r转换为%0D
我写的代码是ruby.我试图帮助Addressable::URI但没有帮助.其他方式可以分别手动将\n和\ r \n替换为%0A和%0D.但是,置换能代替有效的字符,如last\night到last%0Aight,我不想要的.有谁能建议更好的解决方案?谢谢.
old*_*god 10
关于什么 CGI::escape
您只需要编码参数.
url = "http://xyz.com/hello?"
params = "name=john&msg=hello\nJohn\n\rgoodmorning¬e=last\night I went to \roger"
puts "#{url}#{CGI::escape(params)}"
# => "http://xyz.com/hello?name%3Djohn%26msg%3Dhello%0AJohn%0A%0Dgoodmorning%26note%3Dlast%0Aight+I+went+to+%0Doger"
Run Code Online (Sandbox Code Playgroud)