UXP "Network Request Failed" | Uploading File Through Fetch & Xhr

I’m facing an issue while uploading a file to AWS using fetch and FormData. I’m able to read the file successfully, but the upload fails with an error.
The upload request is sent, but in the Network tab the request payload shows as empty, and returns an error(Failed).

Below is the code I’m using. Kindly help me understand what I’m doing wrong and how to resolve this issue.

try {
const ext = CurrentFile.Name.split(“.”).pop().toLowerCase();

/* STEP 1: Save document */
await core.executeAsModal(async () => {
const doc = app.activeDocument;
await doc.flatten();
await doc.save(CurrentFile.Filepath);
});

/* STEP 2: Get AWS Access Token */
const access_token = await get_AWS_Access_Token();
console.log(“AWS Access Token:”, access_token);

const filename = CurrentFile.Name;

/* STEP 3: Init upload (get X-Upload-ID) */
const init = await AWSUploadInitToken(access_token, filename);
console.log(“Upload ID:”, init.upload_id);

/* STEP 4: Read file as binary */
const binary = await CurrentFile.Filepath.read({
format: formats.binary,
});

console.log(
“File read successfully:”,
CurrentFile.Filepath.nativePath
);
console.log(“Binary data:”, binary);

/* STEP 5: Prepare FormData */
let formData = new FormData();
formData.append(“file”, binary, CurrentFile.Name);

console.log(“FormData prepared:”, formData);

/* STEP 6: Upload to AWS */
const res = await fetch(aws_Upload_url, {
method: “POST”,
headers: {
Authorization: Bearer ${access_token},
“X-Upload-ID”: init.upload_id,
},
body: formData,
});

const responseText = await res.text();
console.log(“Upload response text:”, responseText);

if (!res.ok) {
throw new Error(Upload failed: ${res.status} - ${responseText});
}

const result = JSON.parse(responseText);
console.log(“Upload result:”, result);

} catch (err) {
console.error(“❌ Upload failed:”, err);
}

URL → aws_Upload_url: “http://**.***.***.***.8000/upload”,

What I’ve verified

  • File is being read successfully

  • AWS access token is valid

  • Upload ID (X-Upload-ID) is returned correctly

  • FormData is created without errors

Problem

The request fails during the upload step, and AWS returns an error response.

Issue Resolved after http changed to https

Thank you :slight_smile:

1 Like