I love how easy ColdFusion makes it to be productive, but some things could be a little easier.

Much like the fetch command in javascript, CFML engines have cfhttp. Small requests like a simple GET request are easy but as your request gains complexity so do the cfhttpparams  required to make your request work. This wrapper is an attempt to take that complexity out of the equation and will allow you to focus on the important bits... making your application work.

Many of the wrappers member functions return an instance of the component, allowing you to "chain" the setters once it has been instantiated. Here are a few simple examples outlining how it is used.

Here is the complete list of member functions as well as the return value:

  • setURL: Set the URL for the current request. This will return an instance of the component
  • setTimeout: Set the request timeout (in seconds) for the current request. This will return instance of the component
  • setVerb: GET/POST/PUT/DELETE (GET is the default). This will return an instance of the component
  • setCharset: Defaults to utf-8 charset. This will return an instance of the component
  • setToken: setToken will allow you to replace tokens in the url with a value in the format .setToken(':key=value'). This will return an instance of the component
  • setUrlParam: Add an URL parameter to the URL in the format .setUrlParam('key=value'). This will return an instance of the component
  • setFormField: Add a form field to the form post in the format .setFormField('field=value'). This will return an instance of the component
  • setHeader: Add a header to the current request in the format .setHeader('Content-Type=application/json'). This will return an instance of the component
  • setBody: Assign a ColdFusion structure to the POST body and automatically add the Content-Type: application/json header. This will return an instance of the component
  • execute: this method will resolve URL replacing any tokens with the proper values and will append any query parameters. Once the request has executed the response headers and result will be availble. This will return an instance of the component
  • getMeta: This method will return ALL meta data for the request. This will return a structure
  • getResult: Get the result of the request. This will either return a structure if the result is parsable JSON or text
  • getResponseHeader: Get the headers from the response. This will return a structure
  • getStatusCode: A helper method that inspects the response header for the status code. This method returns a string
  • isJSON: A helper method that determines if the the returned response was JSON. This method returns a boolean

You can check out the code here:

Suggestions welcome.

Edit: I have added the source files to GitHub.