Cowboy provides two hooks. onrequest is called once the request line and headers have been received. onresponse is called just before sending the response.


The onrequest hook is called as soon as Cowboy finishes fetching the request headers. It occurs before any other processing, including routing. It can be used to perform any modification needed on the request object before continuing with the processing. If a reply is sent inside this hook, then Cowboy will move on to the next request, skipping any subsequent handling.

This hook is a function that takes a request object as argument, and returns a request object. This function MUST NOT crash. Cowboy will not send any reply if a crash occurs in this function.

You can specify the onrequest hook when creating the listener, inside the request options.

The following hook function prints the request object everytime a request is received. This can be useful for debugging, for example.

Make sure to always return the last request object obtained.


The onresponse hook is called right before sending the response to the socket. It can be used for the purposes of logging responses, or for modifying the response headers or body. The best example is providing custom error pages.

Note that like the onrequest hook, this function MUST NOT crash. Cowboy may or may not send a reply if this function crashes. If a reply is sent, the hook MUST explicitly provide all headers that are needed.

You can specify the onresponse hook when creating the listener.

The following hook function will provide a custom body for 404 errors when it has not been provided before, and will let Cowboy proceed with the default response otherwise.

Again, make sure to always return the last request object obtained.


See also

Version select