Ruby Forum Ruby > Webrick HTTP Errors?

Posted by Christian Kerth (kerthi)
on 15.05.2008 09:39
Hey

How do i set the HTTP Error Code Header when using webrick.

I found the response.set_error function. What do i need to pass there to
e.g. set a http 404 error?

thx
Posted by GOTO Kentaro (Guest)
on 15.05.2008 11:22
(Received via mailing list)
Use raise to set status in WEBrick.  For example,

require "webrick"
s = WEBrick::HTTPServer.new(:Port => 2000)
s.mount_proc("/"){|req, res|
  res.body = "ok"
  raise WEBrick::HTTPStatus::OK
}
s.mount_proc("/404"){|req, res|
  raise WEBrick::HTTPStatus::NotFound
}
trap("INT"){s.shutdown}
s.start


This server returns 404 for http://localhost:2000/404
but return 200 for other urls.

HTH,

Gotoken

On Thu, May 15, 2008 at 4:39 PM, Christian Kerth