Forum Discussion

smogger914's avatar
9 years ago

Component with endpoint and 3rd party REST API

Hey,

 

I am trying to build a component that does a callout to a 3rd party rest api. I can get a JSON response using POSTMAN with that url. I have build out the endpoint like this:

 

<#assign baseUrl = "http://success.mindtouch.com/@api/deki/site/search?q=sso&limit=2&dream.out.format=json"/>
<#assign req = http.client.request(baseUrl) />
<#assign response = req.call("GET") />

In my component I am calling the endpoint like this:

 

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>	
	var handleSuccess = function(data){
		console.log("success", data);
	}

  var request = $.ajax({ 
    url:"/yboys66435/plugins/custom/zuora/zuora/mindtouch_search",
    type: "GET",
    dataType: "json",
    data&colon; {}
  });

  request.done(handleSuccess);

  request.fail(function(jqXHR, textStatus) {
     console.log('Error.', textStatus);
  });
</script>

Hello

When I go to the page that has the component I am seeing a parsererror in the JS console and am not quite sure how to debug this. 

 

 

When I go to the endpoint in the browser it returns this:

 

lithium.coreapi.webui.template.models.HttpClientResponseTemplateModel@125d6b34

This makes me think that it is running the request and getting the response. But how do I get/use this in the component? Is there a way to print out the contents of the HttpClientResponseTemplateModel? 

 

  • Regarding your endpoint code (see comments):

    <#assign baseUrl = "http://success.mindtouch.com/@api/deki/site/search?q=sso&limit=2&dream.out.format=json"/>
    <#assign req = http.client.request(baseUrl) />

    <#-- if you actually want to use the response with FreeMarker, the JSON has to be converted to a FreeMarker hash like that -->
    <#assign req = req.json() />
    <#-- but careful, if you do that, you cannot simply print the response JSON string with ${...} anymore as FreeMarker hashes are not auto-convertable to strings (e.g. JSON) -->
    <#assign response = req.call("GET") /> <#-- shouldn't this be get(), where did you find that call() method? --> <#assign response = req.get() />

    <#-- for above see http://community.lithium.com/t5/Developers-Knowledge-Base/http-client-FreeMarker-Context-Object/ta-p/80614 --> <#-- and then to output the response you have to do something like --> ${response.content}

    <#-- see http://community.lithium.com/t5/Developers-Knowledge-Base/http-client-response-FreeMarker-context-object/ta-p/80622 -->

    and make sure the MIME type of the endpoint is set to JSON.

     

    From the code you provided I cannot see if you are actually outputting something in the endpoint?

     

  • Regarding your endpoint code (see comments):

    <#assign baseUrl = "http://success.mindtouch.com/@api/deki/site/search?q=sso&limit=2&dream.out.format=json"/>
    <#assign req = http.client.request(baseUrl) />

    <#-- if you actually want to use the response with FreeMarker, the JSON has to be converted to a FreeMarker hash like that -->
    <#assign req = req.json() />
    <#-- but careful, if you do that, you cannot simply print the response JSON string with ${...} anymore as FreeMarker hashes are not auto-convertable to strings (e.g. JSON) -->
    <#assign response = req.call("GET") /> <#-- shouldn't this be get(), where did you find that call() method? --> <#assign response = req.get() />

    <#-- for above see http://community.lithium.com/t5/Developers-Knowledge-Base/http-client-FreeMarker-Context-Object/ta-p/80614 --> <#-- and then to output the response you have to do something like --> ${response.content}

    <#-- see http://community.lithium.com/t5/Developers-Knowledge-Base/http-client-response-FreeMarker-context-object/ta-p/80622 -->

    and make sure the MIME type of the endpoint is set to JSON.

     

    From the code you provided I cannot see if you are actually outputting something in the endpoint?

     

    • Thanks! I was missing the output of the endpoint. It turns out I was getting an error and needed to allow the domain in the community settings. The link about the response context variable was very helpful as well. 

       

       After enabling the domain I got it outputting the json response with this: 

      <#assign baseUrl = "http://success.mindtouch.com/@api/deki/site/search?q=sso&limit=2&dream.out.format=json"/>
      <#assign req = http.client.request(baseUrl) />
      <#assign response = req.call("GET") />
      ${response.content}

      The .call method I found in the sample code here.