Thursday, November 10, 2011

How to check for a HTTP 404 response code from a service

Here's a quick guide to checking for a HTTP 404 response code from a service, using the Vordel Gateway.

The first thing to note is that a "Connection" or a "Connect to URL" filter will return true if the back-end service returns a 404 code, because of the fact that the back-end service has successfully returned a response. You can then handle this 404 as you require. Often, it's a good idea to not return the full gory details of the 404 Not Found response to the client, and instead mask this information with a more friendly message. Or, you may wish to connect to a different URL instead. All of this involves branching on the value of the HTTP response code.

The simplest way to do this is to grab the latest version of the Vordel Gateway and check out the "Switch on Attribute Value" filter. This filter is great way to call a different policy shortcut based on the content of an attribute (in this case http.response.status).

Another way to do this is with a script. Below, I am using a script to check if the value of the http.response.status attribute is "404". If it is, I am branching to a "Set Message" filter which sets a nice friendly message to the client, telling them that the service is not available. Following that, I am using a "Reflect" filter to ensure that a 200 OK response code is returned to the client (not the 404 which came from the service).

Here is what the browser user sees. All of the information from the back-end service is replaced by the "Service not available" message.


This is pretty simple to handle at the Gateway, and it's interesting that, with relatively simple configuration like this, you can deploy the Gateway as Web traffic routing infrastructure.