Forum Discussion

dustin's avatar
dustin
Expert
10 years ago

How to access an endpoint from component using freemarker

I've created a working test where I can access the json content in my endpoint using javascript.  

From this post, it sounds like this is possible with freemarker, however I have yet to find any sample code.

 

Can anyone provide sample code for this?

 

 

4 Replies

  • DougS's avatar
    DougS
    Khoros Oracle
    10 years ago

    You can't access an endpoint from a component server-side (in the freemarker logic). You will need to use javascript to call the endpoint from your component.

     

    Another option you can try is to make your http.client call in your page initialization script then set the result as the value of a request attribute: use the http.request.setAttribute call (that is documented on the http request freemarker context object article) in your page initialization script, then use thehttp.request.attributes.name.get call to retrieve the attribute in your component(s). 

     

    -Doug

  • AdamN's avatar
    AdamN
    Khoros Oracle
    10 years ago

    Hi dustin,

     

    I'm curious what you're trying to accomplish by calling the endpoint via FreeMarker? What I've found in the past is that when I wanted to do something like this, I could instead move my endpoint logic to a FreeMarker macro. I can then call that macro both from the endpoint (if I want to access the results externally) or from a custom component (if I want to access the results internally).

     

    Check out this page for more information:

    http://community.lithium.com/t5/Developers-Knowledge-Base/Creating-FreeMarker-macros-for-custom-components-and-endpoints/ta-p/98658

     

    I hope this helps!

  • luk's avatar
    luk
    Boss
    9 years ago

    @Doug Does that "Another option you can try is to make your http.client call in your page initialization script then set the result as the value of a request attribute" mean that you can access an Endpoint with FreeMarker from within the page init script??

  • DougS's avatar
    DougS
    Khoros Oracle
    9 years ago

    luk wrote:

    @Doug Does that "Another option you can try is to make your http.client call in your page initialization script then set the result as the value of a request attribute" mean that you can access an Endpoint with FreeMarker from within the page init script??


    It does not mean you can directly call an endpoint from your components. As AdamN mentioned before, you can call a macro from your page init script, so you could put your http.client logic in that macro and you would be able to call it from your page init script, as well as from an endpoint. If you only need to use it in your page init script, you could put the http.client logic directly in there instead. You would still need to put the result of the http.client call in a request attribute (see the http.request.setAttribute call and the http.request.attributes.name.get call in the http.request freemarker object)  in order to access the results in components.