Ruby 3.0.0 リファレンスマニュアル > ライブラリ一覧 > net/httpライブラリ > Net::HTTPResponseクラス > code

instance method Net::HTTPResponse#code

code -> String[permalink][rdoc]

HTTP のリザルトコードです。例えば '302' などです。

この値を見ることでレスポンスの種類を判別できますが、レスポンスオブジェクトがどのクラスのインスタンスかを見ることでもレスポンスの種類を判別できます。



require 'net/http'

uri = "http://www.example.com/index.html"
response = Net::HTTP.get_response(URI.parse(uri))
response.code # => "200"