How to fetch XML with REST Api resource in Retool

September 15, 2024

To fetch an XML resource in Retool using the REST API, you first need to create a new REST query by navigating to the Retool interface and selecting the Create Query option. Once there, choose REST API as the resource type and enter the API endpoint that provides the XML data. It's important to set the appropriate method, such as GET, depending on how the API is structured.

Since Retool defaults to expecting JSON responses, you need to adjust the response handling by specifying Raw or Text as the Response Type in the query settings. This allows Retool to retrieve the raw XML response without trying to parse it as JSON.

Once the XML data is fetched, it will likely come in a text format, so you’ll need to process it using JavaScript. You can parse the XML response using DOMParser to convert it into an XML Document object that Retool can work with.

Here is a step-by-step guide on how to fetch an XML resource in Retool using the REST API:

Set up the REST API resource in Retool:

img

Transform the response using the Transform results tab:

img

Transform the raw XML:

const doc = new window.DOMParser().parseFromString(data.rawXml, "text/xml");
const locs = doc.querySelectorAll('loc');
const urls = [...locs].map(d => d.innerHTML);
return urls;

img

This approach allows you to effectively parse and handle XML data directly within Retool using its built-in transformation capabilities.

Have a business need or a project in mind?

We help ambitious companies ride the change