Ruby Net::HTTP - following 301 redirects

前端 未结 5 753
时光说笑
时光说笑 2020-12-05 04:36

My users submit urls (to mixes on mixcloud.com) and my app uses them to perform web requests.

A good url returns a 200 status code:

         


        
5条回答
  •  一个人的身影
    2020-12-05 05:19

    rest-client follows the redirections for GET and HEAD requests without any additional configuration. It works very nice.

    • for result codes between 200 and 207, a RestClient::Response will be returned
    • for result codes 301, 302 or 307, the redirection will be followed if the request is a GET or a HEAD
    • for result code 303, the redirection will be followed and the request transformed into a GET

    example of usage:

    require 'rest-client'
    
    RestClient.get 'http://example.com/resource'
    

    The rest-client README also gives an example of following redirects with POST requests:

    begin
      RestClient.post('http://example.com/redirect', 'body')
    rescue RestClient::MovedPermanently,
           RestClient::Found,
           RestClient::TemporaryRedirect => err
      err.response.follow_redirection
    end
    

提交回复
热议问题