Build a new connector with Postman

3. Make a request

In this example, we will create a request to get data from an Atom XML feed from a website URL, then parse the response into the correct format for the status-list Card Template.

Creating a new request

To retrieve the data we want to display as a Card, we must create a new Postman request. If you used the Postman collection creation tool in the previous step, you can alternatively edit an existing request.

To create a new request, click the New button and select Request. Give your request an appropriate name and select your collection as the location to save to, then click Save.

You will now be presented with the request editor screen.

You can send any type of HTTP request to an API if supported; in this example, we will be getting data from a webpage, so the request type has been set to GET.

In the request URL field, enter the URL of the resource or API endpoint you are retreiving data from. Click Send to get a preview of the response.

Modifying the response format

As we are using the status-list Card Template, we will need to create a JavaScript object which matches the template's specified data structure.

In the Tests tab of the request editor, we will insert the following script. An explanation of the script is provided below.

var body = pm.response.text();
var js = xml2Json(body);

var items = js.rss.channel.item;
var value = (items.length > 3) ? 3 : items.length;
var description = `You have ${((items.length>3) ? 3 : items.length)} news items.`;

var model = {
    _card: {
        title: "adenin Blog",
        type: "status-list"
    },
    actionable: true,
    value: value,
    thumbnail: 'https://www.adenin.com/assets/images/identity/Logo_adenin.svg',
    description: description + ' The latest is <b>' + items[0].title + '</b>.',
    items: []
};

// copy top 3 entries
for (var i = 0; i < items.length && i < 3; i++) {
    var raw = items[i];
    var item = {};
    item.id = raw.link;
    item.title = raw.title;
    item.date = new Date(raw.pubDate);
    item.description = raw.description;
    item.link = raw.link;

    // convert thumbnail
    if (raw["media:thumbnail"]) raw.thumbnail = raw["media:thumbnail"]["$"].url;

    model.items.push(item);
}

var template = `<script id="loader" src="https://components.adenin.com/components/at-card-preview/loader.js"></script>`;

pm.visualizer.set(template, model);

Initially, this script takes the body of the Postman request response and uses the module xml2Json to convert it from it's XML format into JSON. We can then use this JSON object to build a new object model, which matches the format required by the Card Template.

Finally, the script includes the adenin sandbox components to enable previewing of the Card in the Visualize tab of the response pane.

For details of defined data fields for Card Templates, as well as information on the notification pane, please refer to the Universal Card Templates reference

To test the request, click Send and navigate to the Visualize tab of the response pane. The rendered Card, alongside with the generated model object is displayed.