Forum Discussion

isxtn's avatar
isxtn
Advisor
12 months ago

Can I call a cURL resource in Freemarker component?

Is there a way to call a cURL resource in a Fremarker component? 

I have a proprietary cURL resource that I want to call in a Freemarker component, that returns JSON. I can't seem to find any reference on how to do this. Is there a way in the Community to do that?

Thanks

4 Replies

  • Sure, here is something simple I did. Someone wanted to display an external RSS feed in their group hub, so I was experimenting with that and used http.client.

     

    I created a custom endpoint in studio and added this code. Inside an endpoint you are using freemarker. The "${response.content}" below is what gets returned when you call this endpoint from your component.

    <#assign baseUrl = "https://api.rss2json.com/v1/api.json?rss_url=http://rss.cnn.com/rss/cnn_topstories.rss"/>
    
    <#assign req = http.client.request(baseUrl) />
    <#assign response = req.call("GET") />
    
    ${response.content}

     

    Then in my component I called the endpoint with this JavaScript and added it to the page from there. I don't think it is possible to call a custom endpoint with freemarker which is why I use JavaScript here. There is an idea about that here There should be a rest call in freemarker for endp... - Atlas (khoros.com)

    <@liaAddScript>
        ;(function($) {
    
            //rss feed endpoint
            const url = "[YOUR CUSTOM ENDPOINT URL GOES HERE]";
    
            //get rss feed
            async function getRssFeed(url) {
                const response = await fetch(url, {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    }
                })
    
                const result = await response.json();
    
                console.log(result);
                feed = result.items;
    
                //display rss feed
                rssDisplay = document.querySelector(".rss-feed-ak");
    
                for (var i = 0;i < feed.length;i++) {
                    var rssItem = document.createElement("div");
                    rssItem.className = "rss-item-ak";
                    rssItem.innerHTML = "<a href='" + feed[i].link + "' target='_blank'><h3>"+ feed[i].title +"</h3></a><div class='rss-image-ak'><img src='"+ feed[i].enclosure.link +"'/></div>"
                    //rssItem.innerHTML = "<a href='" + feed[i].link + "' target='_blank'><h3>"+ feed[i].title +"</h3></a>"
                
                    rssDisplay.appendChild(rssItem);
                }
    
            }
            getRssFeed(url);
    
        })(LITHIUM.jQuery);
    </@liaAddScript>

     

    This is a simple example. If you need to add a body to your call you would use .body() as explained here body("body","body_content_type") (khoros.com) 

    Here's an example from the documentation:

    <#assign http_client_request = http.client.request("http", "www.somewebservicedomain.com", "/some/web/service").body("{ 'application':'lithium', 'user':'johnD' }").get() />

     

    I would read through all the examples http.client.request (khoros.com)

    And one other important thing to note, you have to add the domain you are using to "Allowed Domain Names" in Community Admin, under System > HTTP Client. So, in the last example above I would add "somewebservicedomain.com" to the Allowed Domain Names list.

  • isxtn's avatar
    isxtn
    Advisor
    12 months ago

    Thank you I'll give that a shot Akenefick 

    I just wish they had more extensive documentation.