79092681

Date: 2024-10-16 06:21:02
Score: 0.5
Natty:
Report link

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!

Reasons:
  • Whitelisted phrase (-1): hope it helps
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): facing a similar issue
  • Low reputation (0.5):
Posted by: Kishan Barmawala