Here's an example code snippet to create a HTTP server with a HEAD route in Ruby:
main.rb565 chars27 lines
The code uses the built-in WEBrick library to create an HTTP server listening on port 8000. The server then maps a block of code to the /test
route using mount_proc
.
Inside the block, the req.head?
method is used to determine if the incoming request is a HEAD request. If so, the appropriate headers are set on the response, the status code is set to 200, and an empty response body is returned.
If the incoming request is not a HEAD request, a 400 Bad Request status code is returned with an error message in the response body.
The server is then started and will listen for incoming requests until it is shut down.
gistlibby LogSnag