Gursimrat
12 years agoLeader
Passing parameter from custom component to an endpoint
I am calling an endpoint from a custom component where my requirement is as below:
From a text box, when a user types a query (a string), it will need to be passed in a REST call which will fetch the results based on the correct call.
Below is the custom component code:
<#assign kb_endpoint_url = "/plugins/custom/community-name/community-name/endpointname" />
<@liaAddScript>
(function($) {
$(document).ready(function() {
function callLithiumEndpoint() {
$('.my-div').hide();
var jqxhr = $.get("${kb_endpoint_url}", { search_query: "general"} function(data) {
console.log("success"); 
//alert(data); 
$('.my-div').prepend(data); 
})
.done(function() { console.log("second success"); })
.fail(function() { console.log("error"); })
.always(function() { console.log("finished"); $('.my-div').show(); }); 
} 
callLithiumEndpoint();
});
})(LITHIUM.jQuery);
</@liaAddScript>
<div class="my-div">
</div>Below is the Endpoint code:
<#assign search_query = I_NEED_TO_GET_THE_VALUE_FROM_CUSTOM_COMPONENT_HERE>
<#assign response =  http.client.request("https", "abc@gmail.com:pass123@xyz.zendesk.com","/api/v2/search.json?query=%5C%22${search_query}%5C%22").get() />
<#if response.hasError>
${response.error.message}
<#else>
${response.content}
</#if>
Thanks
- In reference to my last Comment,
 You can accept the parameter in endpoint as,
 <#assign search_query = http.request.parameters.name.get("QueryParameter","") />