Can get ticket search API to work with new FDK9 request templates

Guys,

Any ideas on this. Pre FDK9 I could make a Search API call as follows without any issues. The documentation states the query must be URL encoded.

https://subdomain.freshdesk.com/api/v2/search/tickets?query=“group_id:101000123013%20AND%20created_at:>‘2023-03-24’%20AND%20created_at:<‘2023-04-14’”

Now in FDK9 I am using the following template

  "searchTickets": {
    "schema": {
      "method": "GET",
      "host": "<%= context.subdomain %>.freshdesk.com",
      "path": "/api/v2/search/tickets",
      "headers": {
        "Authorization": "Basic <%= encode(context.apikey) %>"
      },
      "query":{
        "page": "<%= context.page %>",
        "query":"<%= context.queryString %>"
      }
    }

and I pass the same query value in the context of the invokeTemplate call as follows

client.request.invokeTemplate('searchTickets',{
 context:{
    apikey,
    subdomain,
    page:1,
    queryString: `group_id:101000123013%20AND%20created_at:%3E'2023-03-24'%20AND%20created_at:%3C'2023-04-14'`
 }
})

but I get the following error.

{attempts :  1
errorSource : "APP"
headers  : {date: 'Fri, 14 Apr 2023 14:39:25 GMT', content-type: 'application/json; charset=utf-8', transfer-encoding: 'chunked', connection: 'close', status: '400 Bad Request', …}
response : "{\"description\":\"Validation failed\",\"errors\":[{\"field\":\"query\",\"message\":\"Given query is invalid, expected format \\\"keyword:value  OPERATOR keyword:'string' OPERATOR keyword:\\\"string\\\" OPERATOR keyword:>'yyyy-mm-dd' OPERATOR keyword:<integer\\\". Space is mandatory between key/value pair and operator. Please check the paranthesis if there are any.\",\"code\":\"invalid_value\"}]}"

The API call with the query works in Postman so I am happy that it the query is not malformed.

3 Likes

Hey Rob,

Please try the below query and let me know if it works out for you.

request.json

  "searchTickets": {
    "schema": {
      "protocol": "https",
      "method": "GET",
      "host": "<%= iparam.subdomain %>.freshdesk.com",
      "path": "/api/v2/search/tickets",
      "headers": {
        "Authorization": "Basic <%= encode(iparam.api_key) %>",
        "Content-Type": "application/json"
      },
      "query": {
        "query": "\"<%= context.query %>\"",
        "page": "<%= context.page %>"
      }
    }
  }

Invoke function

await $request.invokeTemplate("searchTickets", { context: { query: `group_id:89000021686 AND created_at:>'2023-04-15' AND created_at:< '2023-04-06'`, page },

Hi @mohammed

Thanks for this. I am afraid I still get the error.

Hey Rob,

are you still getting the error? I tried in development app I’m getting 200 response from the API

Server.js

const filterTickets = async () => {
  try {
    let resp = await $request.invokeTemplate("searchTickets", {
      context: { query: `group_id:89000021686 AND created_at:>'2023-04-15' AND created_at:< '2023-04-06'`, page: 1 },
    });
    console.log("filtered tickets :", JSON.parse(resp.response));
  } catch (e) {
    console.log(e);
  }
};

request.json

  "searchTickets": {
    "schema": {
      "protocol": "https",
      "method": "GET",
      "host": "<%= iparam.subdomain %>.freshdesk.com",
      "path": "/api/v2/search/tickets",
      "headers": {
        "Authorization": "Basic <%= encode(iparam.api_key) %>",
        "Content-Type": "application/json"
      },
      "query": {
        "query": "\"<%= context.query %>\"",
        "page": "<%= context.page %>"
      }
    }
  },

Response

Is there some thing I’m missing from your query? let me know

3 Likes

Hi @mohammed

The thing I can see different in your code is that you are doing it in a serverless app whereas I am seeing the error in a client side app, in my case its a custom config page. When creating a custom config page you have to send all values in the context as using the iparam object in the request template otherwise you get a template substitution error.

Got it. So what error are you getting is it a 400 Bad request or error while substituting the template?

I get the following:

{attempts :  1
errorSource : "APP"
headers  : {date: 'Fri, 14 Apr 2023 14:39:25 GMT', content-type: 'application/json; charset=utf-8', transfer-encoding: 'chunked', connection: 'close', status: '400 Bad Request', …}
response : "{\"description\":\"Validation failed\",\"errors\":[{\"field\":\"query\",\"message\":\"Given query is invalid, expected format \\\"keyword:value  OPERATOR keyword:'string' OPERATOR keyword:\\\"string\\\" OPERATOR keyword:>'yyyy-mm-dd' OPERATOR keyword:<integer\\\". Space is mandatory between key/value pair and operator. Please check the paranthesis if there are any.\",\"code\":\"invalid_value\"}]}"

Looks like the error is being thrown from the REST API. The API docs say the query parameter should be URL encoded. Can you try passing the context data after passing it through encodeURI()?

const query = "group_id:89000021686 AND created_at:>'2023-04-15' AND created_at:< '2023-04-06'";

let res = await client.request.invokeTemplate("searchTickets", {
  context: {
    query: encodeURI(query),
    page: 1,
  },
});

Hi @kaustavdm

I tried that. See my original post. Same result.

Hi @kaustavdm

I just tried adding escaped double quotes and I still get the issue

let query = "\"group_id:89000021686 AND created_at:>'2023-04-15' AND created_at:< '2023-04-06'\"")

let response =  await client.request.invokeTemplate('searchTickets',{
   context:{
        apikey,
        subdomain,
         page,
        queryString: encodeURI(query)
    }
})

Curious. @zach_jones_noel thinks it is a bug. We have raised this internally with our engineering team to look into request template substitutions, and they will take it up on priority in the upcoming sprint.

Meanwhile, a similar scenario showed up in:

2 Likes

@RobAtOpinyin

Ran into the same issue. I used this code:

  "getFilteredTickets": {
    "schema": {
      "protocol": "https",
      "method": "GET",
      "host": "<%= iparam.domain %>.freshservice.com",
      "path": "/api/v2/tickets/filter?query=%22status%3A+2+AND+source%3A+1+AND+tag%3A+%27<%= context.tag %>%27%22&page=<%= context.page %>",
      "headers": {
        "Authorization": "Basic <%= encode(iparam.apiKey) %>",
        "Content-Type": "application/json"
      }
    }
  }

It definetly doesn´t looks nice and it isn´t very readable…but it did work.

2 Likes

Yes, thanks for bringing this to our notice @RobAtOpinyin. :raised_hands:

We noticed something off with query params, and our engineering team will be looking into this in deep.

I’ll keep you posted on the bug fix.

1 Like

Hi Guys,

Any news on this?

@zach_jones_noel @kaustavdm

1 Like

@zach_jones_noel @kaustavdm @Saif @Raviraj

Guys,

Reaching out to all of you to see if there is an update on this. Its blocking progress on the development of an app.

1 Like

Hi gang

Also wonder the same thing as @RobAtOpinyin

Hey @RobAtOpinyin and @Ansfs91,

Our team is testing the fix. Will keep you posted when released to production and you can use query params.

2 Likes

Hey @RobAtOpinyin and @Ansfs91,

We have a fix for it, can you share the app IDs and region for the same?

CC @Nadeem_Bhati

With help from @zach_jones_noel and @Nadeem_Bhati I found the solution to this. It works if you add escaped double quotes either side of the context placeholder in the request template.

Shout out to @Ansfs91 who suggested the same solution but I missed that he had manually added encoded double quotes to the request template path in his solution when I tried, so thought it didn’t work. Apologies Albin.

NOTE: If you send the query string in the context object from the code with double quotes, even if encoded with encodeURI(), it will not work. You have to add the double quotes to the request template.

"searchTickets": {
    "schema": {
      "method": "GET",
      "host": "<%= context.subdomain %>.freshdesk.com",
      "path": "/api/v2/search/tickets",
      "headers": {
        "Authorization": "Basic <%= encode(context.apikey) %>"
      },
      "query":{
        "page": "<%= context.page %>",
        "query":"\"<%= context.queryString %>\""
      }
    },
    "options": {
      "retryDelay": 1500,
      "maxAttempts": 5
    }
  }

Call from code

context = {
  apikey: "you_apikey",
  subdomain: "your_subdomain",
  page: 1,
  queryString: "group_id:89000021686 AND created_at:>'2023-04-15' AND created_at:< '2023-04-06'"
 }

await client.request.invokeTemplate ('searchTickets',{context})

`

3 Likes

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