在使用Ruby获取正文之前,如何检查Content-type标头?
|
我需要一种能够获取网页的方式/库,如下例所示:
result = http_client.get(\'/some_page.html\') do |response|
if response.content_type == \'text/html\' and response.code == 200
response.read_body #the headers are returned along with page body
else
#not reading the body so only the headers are returned without body
end
end
现在,如果是带有成功响应的\“ text / html \”页面:
p result.code #>200
p result.content_type #>text/html
p result.body #><DOCTYPE html...
如果是非\“ text / html \”页面或非200页面:
p result.code #>404
p result.content_type #>text/html
p result.body #>nil
所有这些都必须在向Web服务器发出的一个请求中完成。发出HTTP HEAD请求检查标头,然后发出HTTP GET请求获取正文是不可接受的,因为这将导致2个请求。
哪些宝石/图书馆提供了这种可能性?
更新资料
我找到了一个深入到net / http库的解决方案:
client.request_get(uri.request_uri) do |res|
if res.content_type == \'text/html\'
res.read_body
else
res.instance_eval {@body_exist = false}
end
end
没有找到相关结果
已邀请:
2 个回复
擅香
冉案