Adding an extra = at the end of the API string

I need some help.

I have a API that I am calling, because one of these parameters is optional I need to create the parameters part of the string manually. So I created a transformer like this


function createApiString() {
  const params = [];
  
  const selectedPhase = {{ cboPhase.value }};
  const useHistory = {{ useHistoricalVendorCodes.value }};

  if (selectedPhase !== "auto") {
    params.push(`phase=${encodeURIComponent(selectedPhase)}`);
  }
  
  params.push(`intersect_with_vendor_history=${useHistory}`);

  return params.join('&');
}

return createApiString();

The function works and returns the correct string "phase=completion&intersect_with_vendor_history=true"

However, reTool is adding an extra = to the end of the parameters section. I suspect this is because I am not filling in the value part of the URL parameters. Is there a way to suppress this = sign?

If not how do you call API's that have optional parameters?

Hi @jeff_dalgliesh - I get you and am able to reproduce your behaviour.

If I understand correctly, what you want is to get your API endpoint substring to be one parameter less depending on the value of selected phase variable.

I believe there is no need to create additional transformer. Its logic can be inserted in the URL parameters fields.

It may look like this:

Hope this helps!

1 Like

@preshetin brilliant. Thank you it worked perfectly, and just in time for a customer demo that wouldn't have gone as well if it wasn't for your answer. I was working all night on trying to get the frontend talking to the our backend and I just couldn't figure it out. So as a last effort I posted here and in the morning I tried what you posted and everything worked perfectly. Thanks for taking the time to answer, it really, really helped when I needed it.

Respect.
Jeff

1 Like

Happy it worked for you @jeff_dalgliesh !

I suggest you mark my post as Solution so that other people could make use of this Q&A.

P.S. In case you have some tasks to subcontract feel free to send me a private message. I have some time and happy to help

1 Like