API not working in custom app code but works in Postman

Hi team,

I am raising this on behalf of our customer, Tatadigital, from the IND region. They are currently developing an app using our SDK, using API to fetch tickets filtered by status and a custom field value. The code details can be found below.

Domain Name - tatadigitalsit.com

server.js

async function bulkticketResolve(resolveBy, currentMinusOneHrs) {
console.log(“Inside bulkticketResolve Fn”, resolveBy);
console.log(currentMinusOneHrs,‘currentMinusOneHrscurrentMinusOneHrs’);
try {
filter = “status:18”;
var searchTkt = await fd.searchTickets(filter);
console.log(‘after api call searchTickets’);
var searchTktRes = JSON.parse(searchTkt.response);
console.log(JSON.stringify(searchTktRes));
const result = searchTktRes.filter(item =>
item.status == 18
)
const filteredResult = result.map(obj => (
id = obj.id
))
console.log(“filteredResult”);
console.log(JSON.stringify(filteredResult));
} catch (e) {
console.log(‘error inside catch’);
console.log(e);
return false;
}

fd-api-server.js

searchTickets: function (query) {

// /api/v2/search/tickets?query="status:18"
var url = '/api/v2/search/tickets?query="status:18"';
console.log(query )
console.log(url , 'urllllllllll');

return $request.invokeTemplate("getFDapi", {
  "context": {
    "url": url
  },
});

}

The API seems to fetch the expected results in Postman but when used as a part of the code in the app, this led to the following error.

{
status: 400,
headers: {},
response: ‘error while substituting templates.’,
errors: {},
errorSource: ‘APP’,
attempts: 1
}

Can we please check if the code needs to be changed to fix this?

Hi @Vijay_Kannan ,

Greetings!

Thank you for reaching out to the community.

Have you tried making the same request in Postman? It seems there might be an issue with how the data is being passed.

Could you please share your requests.json file for further review?

Thanks,
Anish

This happens when one or more of the variable required for requestTemplate isn’t passed. Please share the requests.json so we can find out what is being missed.

As @sureshprasanna pointed, the template used for making API request might be missing or wrong syntax. Filter API requires the filter params to be URL encoded. It should be URL encoded before using it in the request. Postman will automatically do.

So, could you share the request template used so that we can point out what might be wrong?

Also, could you enter the entire URL along with query params URL encoded passed in the Path in the request template and make it work? After it works, then whatever needs to be passed dynamically can be passed from iparams and context templates.

Some solutions available in the community:

Hi team,

The customer provided request.json for further analysis. Please let us know your thoughts on this.

Resuest .json
{
“verifyFDCredentials”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= context.host%>”,
“path”: “/api/v2/ticket_fields”,
“method”: “GET”,
“headers”: {
“Authorization”: “Basic <%= context.api_key%>”
}
}
},
“postCredentials”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= iparam.tdlDomain %>”,
“path”: “<%= context.url %>”,
“method”: “POST”,
“headers”: {
“client_id” : “<%= iparam.clientId %>”,
“Authorization” : “<%= iparam.tdlApiKey %>”,
“Content-Type” : “text/plain”
}
}
},
“postFDapi”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= iparam.domain %>”,
“path”: “<%= context.url %>”,
“method”: “POST”,
“headers”: {
“Authorization”: “Basic <%= encode(iparam.apiKey + ‘:X’) %>”,
“Content-Type”: “application/json”
}
}
},
“putFDapi”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= iparam.domain %>”,
“path”: “<%= context.url %>”,
“method”: “PUT”,
“headers”: {
“Authorization”: “Basic <%= encode(iparam.apiKey + ‘:X’) %>”,
“Content-Type”: “application/json”
}
}
},
“getFDapi”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= iparam.domain %>”,
“path”: “<%= context.url %>”,
“method”: “GET”,
“headers”: {
“Authorization”: “Basic <%= encode(iparam.apiKey + ‘:X’) %>”,
“Content-Type”: “application/json”
}
}
},
“gettier”: {
“schema”: {
“protocol”: “https”,
“host”: “<%= iparam.tdlDomain %>”,
“path”: “<%= context.url %>”,
“method”: “GET”,
“headers”: {
“client_id” : “CS-WEB-APP”,
“Authorization” : “<%= iparam.tdlApiKey %>”,
“Content-Type”: “application/json”,
“store_id”: “sit.tcp.online”,
“customerHash”: “<%= context.custHash %>”
}
}
}
}

Hi @Vijay_Kannan,

Greetings!

Try without the X

"Basic <%= encode(iparam.apiKey) %>"

This should fix the issue.

Hope that helps, let me otherwise.

Thank you.

The encode, does the btoa internally so you can use it without the :X.