Wednesday, April 15, 2009

Accessing CFHTTPPARAM Type of Body

I'm working on a project with Jason Blum and he's digging into REST web services with CFML. I'll be the first to admit that REST is something I haven't had to deal with much, so I'm glad to be learning from his research on all of this.

To make something truly RESTful, apparently you should be using a simple HTTP request's body as opposed to something like form fields when you do a post to the server. (Again, I'm a moron on all this stuff so if that's way off-base, I'm happy to learn more/be corrected.)

In CFML terms, typically you'll use CFHTTP and CFHTTPPARAM to make the call to the REST service, and use a CFHTTPPARAM type of BODY. The weird thing Jason was running into is how to get at the body content since it doesn't come through in any of the usual scopes.

Jason found a comment by Dan Switzer on a post on Mike Nimer's blog that provided the answer--you simply have to call getHttpRequestData(), which returns a struct, and the struct key CONTENT contains the body data.

Mostly blogging for my own future reference but thought others might find it helpful too.


Nice coincidence. I just learned this yesterday also. I had to create a page that will have xml posted to it. To simulate the post, I have cfhttpparam type="XML" value="#myXMLString#". The page that receives the post uses getHttpRequestData().content to get that xml file and parse it.

Some of the header info available in there can be helpful too.

No comments: