Ruby/Rails - 错误的 URI
不知道为什么当 URI 在浏览器中正常工作时我会收到以下错误:
http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps
这是我的代码:
def kb(to)
uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=#{to.strip}&u=1&p=google-apps"
doc = Nokogiri::XML(open(uri)) # throws error on this line
return parse(doc)
end
我收到以下错误:
in `split': bad URI(is not URI?): http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps (URI::InvalidURIError)`
我按以下方式执行该方法:
kb("Tom Cruise")
Not sure why I'm getting the following error when the URI works just fine in the browser:
http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps
This is my code:
def kb(to)
uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=#{to.strip}&u=1&p=google-apps"
doc = Nokogiri::XML(open(uri)) # throws error on this line
return parse(doc)
end
I get the following error:
in `split': bad URI(is not URI?): http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps (URI::InvalidURIError)`
I execute the method in the following way:
kb("Tom Cruise")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为浏览器是病态友好的,就像小狗一样,会不遗余力地呈现页面或解析 URL。应用程序不会这样做,因为您必须告诉它如何保持友好。
您的 URL 无效,因为它嵌入了空格。将空格替换为
%20
:转义需要转义的字符很容易:
It's because a browser is pathologically friendly, like a puppy, and will go to great lengths to render a page or resolve a URL. An application won't do that because you have to tell it how to be friendly.
Your URL is not valid because it has embedded spaces. Replace the spaces with
%20
:Escaping the characters needing to be escaped is easy: