ColdFusion and the Basecamp API · 262 words posted 03/26/2006 08:31 PM

37signals has released an API for Basecamp, the project collaboration tool. If you use Curl, here’s a command line example for listing your projects (line breaks inserted for clarity):

curl
-H 'Accept: application/xml'
-H 'Content-Type: application/xml'
-u myname:mypassword -i
http://since1968.projectpath.com/project/list

But if you’re not hip to the command line, here’s a ColdFusion example of the same query:

<cfhttp url=
"http://since1968.projectpath.com/projects/42022/milestones/list"
method="get"
result="myresult"
username="myname" password="mypass">
<cfhttpparam type="header" name="Accept" value="application/xml" />
<cfhttpparam type="header" name="Content-Type" value="application/xml" />
</cfhttp>

ColdFusion stores the results in a variable called myresult:

<cfdump var="#myresult#" />

You’ll need to parse the XML stored in myresult.filecontents to make the information human-readable. For example, here’s a quick and dirty loop through the milestone titles, adapted from the ColdFusion XPath docs:

<cfscript>
myxmldoc = XmlParse(myresult.filecontent);
selectedElements = XmlSearch(myxmldoc, "/milestones/milestone/title");
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
writeoutput(selectedElements[i].XmlText & "<br>");
</cfscript>

Basecamp also accepts XML strings to refine your query results, but I haven’t yet figured out how to post them via CFHTTP. If anyone’s done it, feel free to follow up in the comments.

Update: As Dan Short noted here, you can save your XML with CFSAVECONTENT and pass it with another CFHTTPPARAM:

<cfhttpparam type="body" name="post" encoded="no" value="#PostVar#" />

The trick is to make sure you use the name parameter. Thanks Dan.

* * *


1. On Mar 26, 09:48 PM Rob Gonda said:
I wrote a XML-RPC ping function that sends xml, and it’s working great.
The following code will send XML throgh cfhttp

[cfoutput]
[cfsavecontent variable=”xmlData”]
[?xml version=”1.0”?]
[methodCall]
[methodName]stuff[/methodName]
[/methodCall]
[/cfsavecontent]
[/cfoutput]

[cfhttp method=”post” url=”#arguments.rpcurl#” port=”#arguments.rpcport#” throwonerror=”false”]
[cfhttpparam type=”HEADER” name=”User_Agent” value=”ColdFusion” /]
[cfhttpparam type=”HEADER” name=”Host” value=”#cgi.http_host#” /]
[cfhttpparam type=”HEADER” name=”Content-Length” value=”#len(trim(xmlData))#” /]
[cfhttpparam type=”XML” value=”#xmlData#” /]
[/cfhttp]

Sorry about the brackets, but it wouldn’t take code.

Cheers,

~Rob #

2. On Mar 26, 10:01 PM since1968 said:
Thanks Rob; I tried something similar but the Basecamp API doesn’t recognize the XML when it’s passed like that. #