Dynamic URL Construction: Query String Issue with ?
being Replaced by %3F
- Goal:
I am trying to dynamically construct a URL by adding query string parameters based on the values filled in certain fields. The expected behavior is to append only the parameters that have a value.
For example:
If item_id = 123
and group_id = 456
, the final URL should look like:
https://example.com/api/v1/?page=1&item_id=123&group_id=456
.
- Problem:
The issue I'm encountering is that the ?
character in the query string is being replaced by %3F
when the request is constructed. This results in an incorrect URL like:
https://example.com/api/resource%3Fpage=1&item_id=123&group_id=456
.
- Steps Taken So Far:
- I created a transformer to dynamically construct the query string based on the fields:
let attributes = [];
let page = 1;
if ({{ item_id.value }}) {
attributes.push('item_id=' + {{ item_id.value }});
}
if ({{ group_id.value }}) {
attributes.push('group_id=' + {{ group_id.value }});
}
const final_attributes = attributes.length > 0
? '?page=' + page + '&' + attributes.join('&')
: '?page=' + page;
return final_attributes;
- Details:
- It seems like Retool is encoding the
?
as if it were part of the URL path instead of treating it as the start of a query string. - I’ve tried adding the
?
directly in the base URL or in the transformer but neither approach has resolved the issue.
- Questions:
- How can I ensure that the
?
in the query string is not replaced with%3F
when Retool constructs the request? - Is there a better way to handle dynamic query string generation in Retool without this issue?
Received API JSON example:
{
"pagination": {
"page": 1,
"limit": 10,
"totalItems": 10,
"totalPages": 1,
"previousPage": 1,
"nextPage": 2
},
"filters": {
"item": {
"label": "ITEM",
"fields": [
"item_id",
"item_type"
]
},
"group": {
"label": "GRUPO",
"fields": [
"group_id",
"group_type"
]
},
"order": {
"label": "ORDER",
"fields": "order_id"
},
"action": {
"label": "ACTION",
"fields": "last_action"
},
"sender": {
"label": "SENDER",
"fields": "sender_id"
},
"receiver": {
"label": "RECEIVER",
"fields": "recipient_id"
},
"price": {
"label": "PRICE",
"fields": "price"
},
"taxes": {
"label": "TAXES",
"fields": [
"tax_reseller_percentage",
"fee_producer_percentage",
"tax_service_percentage",
"tax_service_fixed"
]
},
"status": {
"label": "STATUS",
"fields": "status"
},
"createdAt": {
"label": "CREATED AT",
"fields": "created_at"
},
"updatedAt": {
"label": "UPDATED AT",
"fields": "updated_at"
},
"archive": {
"label": "ARCHIVE",
"fields": [
"last_action",
"reason",
"archived_at"
]
}
},
"items": [
{
"resale_id": "7f18f6b1-cbfd-4091-a4c0-297696086219",
"created_at": "2024-11-19T02:20:24.380Z",
"updated_at": "2024-11-19T03:14:56.739Z",
"sender_id": 26203862,
"recipient_id": 30264748,
"item_id": "123123123123123",
"item_type": "ticket",
"group_id": "93297",
"group_type": "event",
"order_id": "123123123123",
"status": "TRANSFERRING",
"resale": false,
"price": null,
"tax_reseller_percentage": null,
"fee_producer_percentage": null,
"tax_service_percentage": null,
"tax_service_fixed": null,
"last_action": "TRANSFER_MADE",
"reason": "transfer completed successfully",
"archived_at": "2024-11-19T03:15:21.399Z"
},
{
"resale_id": "a186a812-9ea1-442d-96f3-cb31e5c0a249",
"created_at": "2024-11-18T17:14:38.086Z",
"updated_at": "2024-11-18T18:19:27.417Z",
"sender_id": 1851170,
"recipient_id": 19772886,
"item_id": "123123123123123",
"item_type": "ticket",
"group_id": "93338",
"group_type": "event",
"order_id": "123123123123",
"status": "TRANSFERRING",
"resale": false,
"price": null,
"tax_reseller_percentage": null,
"fee_producer_percentage": null,
"tax_service_percentage": null,
"tax_service_fixed": null,
"last_action": "TRANSFER_MADE",
"reason": "transfer completed successfully",
"archived_at": "2024-11-18T18:19:51.650Z"
}
]
}