Sample ColdFusion Code Using CFHTTP

The code below is an example of how to call the Command API through its HTTP/XML interface from ColdFusion. This is how you would invoke the API from outside an existing CommonSpot site, or from another server. These principles and code flow can also be used in other programming languages, though the syntax will be different of course.

Nearly all commands in the Command API require authentication, so the first task is to programmatically log in. To do this, call Login.doLogin, passing the user name and password of a valid CommonSpot contributor.

As with any CFHTTP call, you first want to check that the request succeeded at the HTTP level, i.e., that the URL is correct, the server is accessible, etc.. See below for an example.

If the request itself succeeded, you next want to check the CommonSpot didn't refuse the command for some reason, or throw an error. To do this, check the 'code' key in the status structure returned by the command. Again see below for an example.

If the command ran successfully, next check the data it returned. Login.doLogin returns a structure, whose LoginResult key indicates the status of the login attempt. As shown in the documentation for this command, 0 means login failed, 1 means it succeeded, and 2 means it succeeded but a password change is required. Your code should react appropriately, depending on your application.

Also in the result struct is a SessionCookies key, also a structure, containing cfID, cfToken, and jSessionID keys. You need to pass these back as cookie parameters to subsequent commands, in order to maintain the logged-in user's ColdFusion session. It's probably easiest to capture them into a session variable, and reference that in later API calls, as shown below.