Response: 'Error while substituting the templates', status: 400, headers: {…}, errorSource: 'APP' when triggering RestAPI's

    var headers = {"Authorization": "Basic <%= encode(iparam.api_key) %>"};
    var options = { headers: headers, body: "Hello world"};
    var url = "https://newaccount1633786790718.freshdesk.com/api/v2/tickets/6.json";
    client.request.put(url, options).then (
    function(data) {
      console.log(data);
    },
    function(error) {
      console.log(error);
    });

I saw other solutions in the forum like reauthenticate the credentials but not worked for me,

Tried another code

 var headers = {"Authorization": "Basic <%= encode(iparam.api_key) %>"};
 client.request.put("https://newaccount1633786790718.freshdesk.com/api/v2/tickets/6",{'name':'hello'},headers).then (
    function(data) {
      console.log(data);
    },
    function(error) {
      console.log(error);
    });

Above code throws the error like **staus:401, headers: {…}, response:

'{"code":"invalid_credentials","message":"You have to be logged in to perform this action."}', attempts: 1, errorSource: 'APP'}

checked with “whitelisted-domains” in the manifest file but error not solved :neutral_face:

Hi @yashwanth_balanagu

Hope you are safe.

Please rewrite your code in the following way and check

let data = { "name":"hello"}

let response = await client.request.put("https://newaccount1633786790718.freshdesk.com/api/v2/tickets/6",{
headers: { Authorization: "Basic <%= encode(iparam.api_key) %>", "Content-Type": "application/json"},
body: JSON.stringify(data)
});

console.log(response);

Let me know if it doesn’t work

Regards,
Mughela Chandresh

4 Likes

Please refer to this sample code L26 - L36 - https://github.com/freshworks-developers/request-method-samples/blob/2680ac9ea2d58ca4077f70784a35ba255f0fb8ab/freshdesk/app/scripts/app.js#L26-L36

This has been tested with the app and API key based auth succeed.

The procedure is accurately described by @Mughela_Chandresh :arrow_heading_up:

2 Likes

//Inserted code

let data = { "name":"hello"}

let response = await client.request.put("https://newaccount1633786790718.freshdesk.com/api/v2/tickets/6",{
headers: { Authorization: "Basic <%= encode(iparam.api_key) %>", "Content-Type": "application/json"},
body: JSON.stringify(data)
});

Result:- App.js:32 Uncaught (in promise) {response: ‘Error while substituting the templates’, status: 400, headers: {…}, errorSource: ‘APP’}

Result:- App.js:32 Uncaught (in promise) {response: ‘Error while substituting the templates’, status: 400, headers: {…}, errorSource: ‘APP’}

Coming error @Saif suggested by @Mughela_Chandresh

One of the things that can help us make progress is by looking into the logs. As you make this API call, can you share the fdk.log file that is generated? — Especially those log lines that indicate this API call is being made.

See how fdk.log helps in troubleshooting

Copy specific section of logs using Debug Mode

Share browser logs if you think those can help.

Also, do you mind sharing you iparams.json ?

1 Like

Could you ensure a couple of things?

  1. The domain has to be whitelisted by adding the domain part alone to the manifest.json file in the app root directory under the “whitelisted-domains” property. You can check the example.
  2. The “api_key” should be an installation parameter key in your iparams.json file and it has been filled with value on the local testing page or tried end-to-end local testing by navigating to the Apps page in the Freshworks Product. The steps can be found here.

The error could most probably be caused by the second point. Could you check if you have the right installation parameter in your app and fill its value in either simulation way?

If you could share the contents of your iparams.json file, we can help you out as well.

Thanks for sharing the app source code over the Personal Message thread.

Summarizing here for future visitors.

  • The code has been adopted from somewhere and the api_key is expected from the installation parameters in the app.
  • The app source code actually had freshdesk_api_key as the installation parameter to get the Freshdesk’s API key from the user.
  • So changing the installation parameter’s name in the Authorization request header fixed the issue.
2 Likes

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.