Geeting error related route function is being imported or used within Forge app

ERROR   2024-07-02T13:00:11.982Z 95169c9c-f986-46c7-a005-51e4278d4a8f Exception occurred: (0 , _forge_api__WEBPACK_IMPORTED_MODULE_1__.route)(...).fetch is not a function
PS C:\Users\kindinti.anumanthu\OneDrive - BLACKLINE\Desktop\Automation\JiraPRTesting>
import ForgeUI, { render, Fragment, Text, Button, Strong, Form, Select, Option, IssuePanel, useProductContext, useState } from '@forge/ui';
import { route } from '@forge/api';
import { Buffer } from 'buffer';


const auth = Buffer.from(`${jenkinsUser}:${jenkinsToken}`).toString('base64');

try {
    const response = await route(buildUrl).fetch({
        method: 'POST',
        headers: {
            'Authorization': `Basic ${auth}`,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams(parameters).toString()
    });

    console.log(`Request URL: ${buildUrl}`);
    console.log(`Response: ${response.status} - ${response.statusText}`);
    console.log(`Response Content: ${await response.text()}`);

Hi @KindintiAnumanthu , the problem here is that the route function doesn’t return an object with a fetch property that is a function. Also, you will need to make sure that you use our version of the fetch function available via @forge/api

What you will want to do is something like this:

import { fetch } from '@forge/api';

const result = await fetch(buildUrl, {
        method: 'POST',
        headers: {
            'Authorization': `Basic ${auth}`,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: new URLSearchParams(parameters).toString()
});

The documentation can be found here.

1 Like