0

I am following Jimm Stout's suggestion for websites that don't set content-type.

  agent = Mechanize.new do |a|
    a.post_connect_hooks << ->(_,_,response,_) do
      if response.content_type.empty?
        response.content_type = 'text/html'
      end
    end
  end

How do I avoid setting the Content-Type if I get a redirect, a 40x or a 50x.

zhon
  • 1,610
  • 1
  • 22
  • 31

1 Answers1

0

You can make sure the response class is Net::HTTPOK.

agent = Mechanize.new do |a|
  a.post_connect_hooks << ->(_,_,response,_) do
    break unless response.class == Net::HTTPOK
    if response.content_type.empty?
      response.content_type = 'text/html'
    end
  end
end
zhon
  • 1,610
  • 1
  • 22
  • 31