Unable to POST to Another Jira Instance using Fetch

I am trying to create a forge request to update data from 1 Jira Cloud instance to another Jira Cloud Instance. I’ve added external.fetch.backend in my manifest file:

  external:
    fetch:
      backend:
        - 'https://xyz.atlassian.net'

Code snippet to update the Issue in Instance 2:

async function processUpdate(issueKey, fieldMappings, subDomain) {
  const updateBody = JSON.stringify(fieldMappings);

  try {
    const response = await fetch(`https://${subDomain}.atlassian.net/rest/api/3/issue/${issueKey}`, {
      method: "PUT",
      headers: {
        Authorization: `Basic ${Buffer.from(credentials).toString("base64")}`,
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: updateBody,
    });

    if (!response.ok) {
      const errorMessage = await response.text();
      throw new Error(`Failed to update issue. Status: ${response.status} ${response.statusText}. Error: ${errorMessage}`);
    }

    console.log(`${response.status} ${response.statusText}`);
  } catch (err) {
    console.error(`Error updating issue ${issueKey}:`, err);
  }
}

I am getting the credentials from dotenv.config()
The app works fine if I am running using Tunnel, but when I install it, it gives below error in the Developer console logs:

Error updating issue ABC-xx:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
    at new NodeError (node:internal/errors:405:5)
    at Function.from (node:buffer:325:9)
    at processUpdate (webpack://product-trigger/src/controllers/updateIssue.jsx:38:40)
    at updateIssue (webpack://product-trigger/src/controllers/updateIssue.jsx:28:3)
    at Object.run (webpack://product-trigger/src/index.jsx:35:5)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async r.<computed> [as handler] (/var/task/index.cjs:2:511237) {
  code: 'ERR_INVALID_ARG_TYPE'
}

I tried putting credentials directly as well. Then, it does not give an error, but it does not update the issue in the 2nd instance.

So :
1. If I trigger the code using tunnel, it works perfectly.
2. If I trigger it when installed in instance without tunnel, it gives error as I mentioned above.
3. If I directly put the credentials inside Buffer as string, it neither gives an error, not it updates the issue in instance 2.

How do I make it work?

SOLVED: Very Basic Mistake by me, Just needed to put Authorization under quotes. That’s it!

2 Likes