Spreading Http And HttpParam Tags Across Multiple Function Calls In Lucee CFML 126.96.36.199
Back when InVision was running on Adobe ColdFusion 10, I leaned heavily on the native
Http.cfc component when making network requests. However, once we switched over to Lucee CFML 5.3, where tag support in CFScript is much more robust, I found myself reverting back to the
httpParam tags for network requests. There's something I find so elegant about the syntax of tags and their builder-like pattern of construction. One nice about using the
Http.cfc ColdFusion component, however, is that it can be passed around and augmented such as with the injection of tracing headers. This got me thinking about augmenting
http tags with
httpParam tags; and, whether such an augmentation could be spread across function calls in Lucee CFML 188.8.131.52.
This isn't the first time that I've considered such a pattern. Last year, I demonstrated that you could compose a SQL statement along with its
cfqueryparam tags across multiple function calls. At the time, I didn't know if that was something specific to the
cfquery tags; or, if that was a technique that would generally work with all tags in Lucee CFML.
To explore this concept, I'm going to create a page that composes
httpParam tags inside Function calls. Then, I'm going to make an HTTP request to an end-point that echoes the incoming HTTP headers in its response. First, let's just take a look at the
target.cfm template - our HTTP end-point:
<cfscript> // Extract inbound HTTP headers. incomingHeaders = getHttpRequestData( false ).headers; // Prepare HTTP headers to be echoed in BODY of response. serializedHeaders = serializeJson( incomingHeaders ); responsePayload = charsetDecode( serializedHeaders, "utf8" ); content type = "application/json; charset=utf8" variable = responsePayload ; </cfscript>
As you can see, this simply grabs the
Now, we can go about generating that request using the
http tag and several
httpParam tags. Note that two of the
httpParam tags are defined in UDFs (User Defined Functions):
As you can see, inside our
http tag context, we're calling two methods:
Each of these methods contains an
httpParam tag that magically attaches to and augments the
http context in current the call-stack. And, when we run this ColdFusion template, we get the following output:
As you can see, all three
httpParam header values were attached to the outgoing HTTP request. This demonstrates that
httpParam tags can be aggregated across several method calls. This means that we can easily create functions that apply "behaviors" (such as Authorization and Tracing) to any contextual
http tag in our application.
Freaking Lucee CFML - it's just awesome!
Want to use code from this post? Check out the license.
You should check out HYPER - http calls with a real chainable builder setup, its so cool in CFML.
That looks pretty cool. I feel like I've heard you all talk about that on the Modernize or Die show. Looks like it has a lot of flexibility. And, the ability to set defaults across the board is very nice. 💪
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →