Passing dynamic query params to API resource

I looked in the community for an answer to this and the suggested solutions (1, 2) do not solve my problem.

I am calling a GET endpoint and need to add an arbitrary set of query params to it.

?foo=bar&foo1=bar1&foo2=bar2

I am creating this query string using a transformer as it requires references to several components on the page.

Within the query, I have a path parameter value and then I want to simply add the query parameter string to it.

api/items/{{ pathParam.value }}?{{ queryParams.value }}

However, when doing this it autopopulates the query parameter within the query, only setting the key with the transformer value

As a result, the full request url includes a dangling "=".

This is bad because our API, correctly, throws an error.

{
      "_tag": "Transformation",
      "path": [
        "work_score_weight"
      ],
      "message": "Unable to decode \"0.65=\" into a number"
    },

The other thing I tried was the following:

api/items/{{ pathParam.value + '?' + queryParams.value }}

This avoids the autopopulation of the query param in the query

Yet the ? character, which is the separating delimiter, is now encoded in the resulting query, causing an error.

Please advise.

Hi @Eriks_Reks,

I did a little testing and found you are correct. It seems there is a bug which add a trailing =. See this

I did find a workaround using a JS query instead. Here is a sample to get you started:

const url = `https://api.hybiscus.dev/api/v1/items/api/items/${pathParam.value}?${queryParams.value}`;

const options = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    // Add additional headers if needed
  },
  body: JSON.stringify({
    // Your body here
  }),
};

console.log("Request URL:", url);
console.log("Request Options:", options);

return fetch(url, options).then(res => res.json());

As you can see the request is now correct:
chrome_OC8NaPNSC5

I hope this helps!

1 Like

Thanks @Shawn_Optipath ! Appreciate the suggestion. I will do exactly this.

1 Like