Do you have any ideas for dynamically generating the URL used in a Call REST service action in Mendix version 10.24.0?

0
I am creating a search screen in Mendix, and I am trying to use a REST API to retrieve data from the database.There are 10 search boxes, allowing the user to apply up to 10 search conditions.These search parameters need to be appended to the end of a base URL in order to retrieve the target data. (For example, if the itemsCode and itemsRegulation search boxes have values, the URL would look like this:https://apps./api/info?itemsCode=aaaaa&itemsRegulation=bbbbb.Only the boxes that have values should be added as parameters, and any empty boxes must not be included in the URL.) My initial idea for dynamically generating this URL was to build it in a microflow by repeatedly checking whether each search box is empty and then branching accordingly.However, this would require an enormous number of conditions, so I am looking for a better way to achieve this.Since I am a beginner, I haven’t been able to come up with an alternative approach yet, so I would like to ask the brilliant minds of the world for their advice. Thank you very much for your help.
asked
2 answers
5

There are a couple of ways to build a dynamic URL in Mendix when you’ve got multiple search parameters. Let’s say $SearchCriteria is a object with fields like items, itemsCode, itemCodeCount, etc.

 

Option 1 is to build the URL in a microflow using multiple Change Variable activities.

First, create a variable called 'QueryString' with an initial empty value (''). Then add a series of Change Variable actions — one for each search parameter. Each activity will look something like this (adjusted per field):

 

If $SearchCriteria/ItemsCode != empty then $QueryString + (if $QueryString != '' then '&' else '') + 'itemsCode=' + $SearchCriteria/ItemsCode else ''

                                           ---------------                            

Option 2 is to use a Java or JavaScript action that returns the queryString, which you can then append to your base URL. Below is code for javascript action: 

 

let attributes = searchCriteria.getAttributes();

let queryParams = [];

// Loop through the attributes

attributes.forEach(key => {

    let value = searchCriteria.get(key);

    if (value && value.trim() !== "") {

          queryParams.push(${encodeURIComponent(key)}=${encodeURIComponent(value.trim())}); }

     });

// Combine the query params

let queryString = queryParams.join("&");

return queryString;

answered
2

Use a helper entity + loop + string concatenation in a microflow:

 

Create a helper entity (e.g., SearchParameter) with attributes:Name (string): the query parameter name (like itemsCode)Value (string): the value from the search box.

 

In your microflow, before calling the REST API:Create a list of SearchParameter objects — one for each search box with a non-empty value.For example, check each search box; if it has a value, create a SearchParameter with the parameter name and value and add it to the list.

 

Build the URL string dynamically:Initialize a string variable with the base URL, e.g. "https://apps./api/info?"Loop over the list of SearchParameter objectsFor each, append "Name=Value" to the URL string, prefixing with & except for the first oneThis way, only parameters with values get added, and the URL is correctly formattedUse the generated URL to call your REST API

 

Microflow Logic:

Initialize url = "https://apps./api/info?"

Initialize empty list of SearchParameter

For each search box:   

    If box.value is not empty:       

    Create SearchParameter with Name and Value       

    Add to list

For each SearchParameter in list:   

If first parameter:       

    url += Name + "=" + Value   

Else:       

    url += "&" + Name + "=" + Value

Call REST API with url

answered