Using ampersand-separated list for jira API with @forge/api

Hello,

In my resolver i have this

resolver.define("getAllProjectsPaginated", async (req) => {
  const keyParams = "keys=JAO&keys=PFE&keys=TES";
  const maxResults = req.payload?.maxResults || 20;
  const result = await api
      .asApp()
      .requestJira(
          route`/rest/api/3/project/search?${keyParams}&expand=lead&maxResults=${maxResults}`,
          {
            headers: {
              Accept: "application/json",
            },
          },
      );
  const response = await result.json();
  console.log("getAllProjectsPaginated result: ", response);
  return response;
});

And in the dev console i get this response

getAllProjectsPaginated result:
{
  self: 'https://api.atlassian.com/ex/jira/........./rest/api/3/project/search?expand=lead&maxResults=20&keys=JAO&keys=PFE&keys=TES=&startAt=0',
      maxResults: 20,
    startAt: 0,
    total: 8,
    isLast: true,
    values: [ ... ]
}

as you can see it gets all 8 projects that i have in the env instead of just the 3 i specified, even when im calling the endpoint according to the jira api documentation here.

This is not my actual use case since im just specifying a string as

const keyParams = "keys=JAO&keys=PFE&keys=TES";

in reality my use case is more like this

const keys = req.payload?.keys || "";

i put it in strings just so that it can be easier to reproduce.

My question now is, if this doesn’t work how can i have an ampersand-separated list in here as a user input filter to only show certain projects.

Hi @AymaneAitlaasri,

I think the issue you’re running into is that the route tagged template function is encoding the ampersands and equals characters in your keyParams string. As a general rule, you can’t programmatically build query strings and then pass them to route due to this encoding behaviour — it only works for interpolating individual query param values.

Instead of interpolating the values yourself in a string, try using a URLSearchParams object to build up your query string, as in the bottom of the route usage example here.

cheers,
Tim