Why do I have to URI.encode even safe characters for Net::HTTP requests?

北城以北 提交于 2020-01-02 03:50:24

问题


I was trying to send a GET request to Twitter (user ID replaced for privacy reasons) using Net::HTTP:

url = URI.parse("http://api.twitter.com/1/friends/ids.json?user_id=12345")
resp = Net::HTTP.get_response(url)

this throws an exception in Net::HTTP:

NoMethodError: undefined method empty?' for #<URI::HTTP:0x59f5c04> from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1470:ininitialize'

just by coincidence, I stumbled upon a similar code snippet, which used URI.encode prior to URI.parse, so I copied that and tried again:

url = URI.parse(URI.encode("http://api.twitter.com/1/friends/ids.json?user_id=12345"))
resp = Net::HTTP.get_response(url)

now it works fine, but why? There are no reserved characters that need escaping in the URL I mentioned, so why do I have to call URI.encode for get_response to succeed?


回答1:


Ruby 1.8 get seems to require a forward slash at the end of the uri.

"http://www.google.com/"

worked, while

"http://www.google.com"

did not.



来源:https://stackoverflow.com/questions/2423827/why-do-i-have-to-uri-encode-even-safe-characters-for-nethttp-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!