Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URI Response Code

Tags:

url

ruby

open-uri

I would like to use Ruby's OpenURI to check whether the URL can be properly accessed. So I would like to check its response code (4xx or 5xx means error, etc.) Is it possible to find that?

like image 522
Mika H. Avatar asked Nov 29 '12 21:11

Mika H.


People also ask

What is a URL response code?

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: Informational responses ( 100 – 199 ) Successful responses ( 200 – 299 ) Redirection messages ( 300 – 399 )

What is a 204 response code?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.

What is a 201 response?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

What is a response status code?

HTTP response status codes (or simply status codes) are three-digit codes issued by a server in response to a browser-side request from a client. These status codes serve as a means of quick and concise communication on how the server worked on and responded to the client's request.


1 Answers

You can use the status method to return an array that contains the status code and message.

require "open-uri" 

open("http://www.example.org") do |f|
  puts f.base_uri  #=> http://www.example.org
  puts f.status    #=> ["200", "OK"]
end
like image 175
orde Avatar answered Sep 28 '22 22:09

orde