Blog

Eric Peterson

December 13, 2017

Spread the word


Share your thoughts

We enjoyed bringing you tips for [CommandBox](https://www.ortussolutions.com/blog/category/12-tips-of-commandbox-christmas) and [ContentBox](https://www.ortussolutions.com/blog/category/12-tips-of-contentbox-christmas) last year for the 12 days of Christmas so much that we decided to do it again! This year, I'm happy to show you 12 different modules currently hosted on ForgeBox just waiting to make your life easier. So grab a hot drink and cozy up for the **12 Tips of (ForgeBox) Christmas**.

Hyper

Hyper exists to make your life making network requests easier.

Here's the simplest Hyper example:

var res = hyper.get( "https://swapi.co/api/films/7" );

The HyperResponse object returned has a few helpers to make it easy to work with requests, including retrieving the status code, inspecting headers, and deserializing json bodies.

The more complex you get with your HTTP requests, the more Hyper shines. A POST request looks like so:

var res = hyper.post( "https://swapi.co/api/films", {
    "title" = "The Last Jedi",
    "episode_id" = 8,
    "release_date" = "2018-12-15"
} );

This sends a json body, by default. Need form fields and a custom header?

var res = hyper
    .asFormFields()
    .withHeaders( { "X-Prerelease-Token" = token } )
    .post( "https://swapi.co/api/films", {
        "title" = "The Last Jedi",
        "episode_id" = 8,
        "release_date" = "2018-12-15"
    } );

While this is a nice syntax improvement over cfhttp, Hyper was built to solve the pain points around writing SDK's like GitHub, Stripe, and Twilio. When working with services like these, there is a handful of boilerplate headers, parameters, or other values you need to set.

Hyper lets you create clients — partially configured instances that pass on their defaults to every request made from them. You create a new client with WireBox (most likely in your config/WireBox.cfc):

map( "GitHubClient" )
    .to( "hyper.models.HyperBuilder" )
    .asSingleton()
    .initWith(
        baseUrl = "https://api.github.com",
        headers = {
            "Authorization" = getSetting( "GITHUB_TOKEN" )
        }
    );

This client can then be injected and used elsewhere in your application:

component {

    property name="githubClient" inject="GithubClient";

    function getRepos() {
        return githubClient.get( "/user/repos" );
    }

}

No more repeating yourself all over the place with the same defaults! And if you need to override a default, just set it as normal. Hyper has a full fluent API to set all the parameters you would need.

So check out the documentation over on ForgeBox and stop rewriting the same cfhttp code over and over again.

Merry Christmas!

Add Your Comment

(1)

Dec 14, 2017 06:56:06 UTC

by Chris Geirman

Impressive! I recall there being many nitnoid issues with json deserialization in CF. I don't recall specific pain points however. Assuming you know what I mean, have you solved for them?

Recent Entries

Is Cloud the Answer for Your ColdFusion Dilemma?

Is Cloud the Answer for Your ColdFusion Dilemma?

Feeling the limits of an on-premise ColdFusion setup?

Many businesses face high costs, limited scalability, and performance bottlenecks, leaving them wondering if the cloud could be the answer.

In our FREE whitepaper, "Is Cloud the Answer for Your ColdFusion Dilemma?", we explore:

  • Benefits of Migrating to the Cloud: From cost savings to increased flexibility, find out what a cloud-based ColdFusion setup can do.
Cristobal Escobar
Cristobal Escobar
November 13, 2024
Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024
10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

ColdFusion remains a powerful and versatile platform for building dynamic web applications. However, keeping your ColdFusion environment optimized, secure, and scalable requires specialized expertise. Whether managing a long-standing ColdFusion application or planning new development projects, hiring a dedicated ColdFusion consulting and support team can be a game-changer for CTOs, CIOs, and developers. Here's why:

1. Expert Guidance on ColdFusion Web Development

...

Cristobal Escobar
Cristobal Escobar
November 08, 2024