开发者

How do I check the Content-type header before getting the body using Ruby?

I need a way/library that is able to fetch a web page like in the example below:

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

Now in case of a "text/html" page with a success response:

p result.code #>200
p result.content_type #>text/html
p result.body #><DOCTYPE html...

In case of a non-"text/html" page or non 200 page:

p result.code #>404
p result.content_type #>text/html
p result.body #>nil

It all must be done in one request to t开发者_JAVA百科he web server. It's not acceptable to make a HTTP HEAD request to check the headers and then a HTTP GET request to get the body because it would result in 2 requests.

What gem/library offer such possibilities?

Update

I found a solution digging into the net/http library:

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


I found a solution digging into the net/http library:

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


Maybe an HTTP HEAD returns what you want.

HEAD should be supported, as this link suggests

http://rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜