After some trial and error, I finally got the following TypeScript code to work:
const url = `https://dev.azure.com/${organization}/${projectName}/_apis/distributedtask/securefiles?api-version=7.1-preview.1&name=${fileName}`;
const resolvedPath = path.resolve(filePath);
const fileContent = fs.readFileSync(resolvedPath);
try {
const response = await axios.post(url, fileContent, {
headers: {
"Authorization": `Basic ${Buffer.from(`:${YOUR_PAT_TOKEN}`).toString('base64')}`,
"Content-Type": "application/octet-stream",
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
if (response.status === 200 || response.status === 201) {
console.log(`[SUCCESS] Uploaded secure file: ${fileName}`);
return response.data; // Return response data if needed
} else {
console.error(`[ERROR] Unexpected response status while uploading file: ${response.status}`);
return null;
}
} catch (error) {
console.error("[ERROR] Failed to upload secure file:", error);
throw error;
}
This solution took me some time to figure out, so I hope it helps anyone facing a similar issue. If there are any improvements or better practices, I’m open to suggestions!