Sign in to PrePAN




User's Modules

HTTP::Response::Switch handle many HTTP response possibilities


Sometimes the only possible way to communicate with an online service is through a web application intended for human consumption--dealing with cookies, forms and parsing of HTML, perhaps with the help of WWW::Mechanize and Web::Scraper.

When automating such a web application, it may be unsafe to assume that a specific request will always trigger a specific response. For example, requesting bank transactions from an Internet Banking server could result in a response containing:

  1. a CSV file of bank transactions (the "expected" response);
  2. an HTML page indicating an input error and presenting a form;
  3. an HTML page indicating that the session has been terminated;
  4. an HTML page indicating an Internal Server Error; or
  5. something else entirely unanticipated.

Some of those "unexpected" or "undesired" responses may require special behaviour, in which case it isn't appropriate to simply die on such responses. Moreover, some of those responses could potentially occur on any communication with the web application, creating the need to test for them at every single point that the web server is contacted.

This distribution aims to abstract away some of the code verbosity that would be required for this by providing Moose roles for:

  1. "Handler" classes, which look at a specific HTTP::Response object with a single concern in mind and either return structured data, throw an exception, or indicate that they don't know how to handle that specific response.
  2. A "dispatcher" class which takes an HTTP::Response object and passes it through a chain of the aforementioned "handler" classes until one of them returns structured data or throws an exception; or, if none of them do, indicates that that specific response truly is "unexpected."

This functionality doesn't seem to already exist on CPAN.

Intended usage

After release, I intend to almost immediately make this distribution a dependency of:

  1. Finance::Bank::Bankwest, which currently inlines a less generic version of this functionality as Finance::Bank::Bankwest::Parsers (and Finance::Bank::Bankwest::Parser)
  2. Finance::Bank::Melbourne, which cannot be published until it can reference this as a dependency


Some of the things I've considered while trying to decide on a name for the distribution:

  • it deals with HTTP::Response objects
  • it's probably only particularly useful when communicating with a web app written by a third party
  • it's relevant when there are multiple different response choices, or when there's a selection of possibilities
  • handlers are called in a defined order, so it's kind of like a chain
  • it could almost be considered a type of switch construct for HTTP::Responses

Accordingly, I came up with these names over the time that I've been thinking about it:

  • HTTP::Response::HandlerChains
  • HTTP::Response::HandlerChain
  • HTTP::Response::Handling
  • HTTP::Response::MultiHandling
  • HTTP::Response::WebAppHandling
  • HTTP::Response::WebAppHandler
  • HTTP::Response::WebAppHandlers
  • HTTP::Response::Choices
  • HTTP::Response::Switch

At this stage, HTTP::Response::Switch seems to be the least terrible name.


  1. Does this functionality in fact already exist somewhere on CPAN?
  2. What name would you suggest for this distribution? One of the above suggestions, or something else entirely?

Lx@github 2 comments