Forum Discussion
- AkenefickGenius
Are you trying to reach an external API (not the community API)? If so, I think there is only one way to do that. You have to use http.client.request from a custom endpoint.
http.client.request (khoros.com)
Tutorial: Integrate with a third-party REST API (khoros.com)
If that's what you are trying to do, I can share some code.
- isxtnAdvisor
Yes, please share some code! 🙂
- AkenefickGenius
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.
Related Content
- 4 years ago
- 13 years agoInactive User