Fetch failed type error while uploading file to sharepoint

Shardul More 5 Reputation points
2024-01-01T12:17:13.79+00:00

Trying to upload files to sharepoint using graph api but getting typeerror fetch failed.It is working fine in local machine but getting above error when moved to gcp virtual machine may be because proxy issue.

// Authenticate using Client Secret credentials
    const credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);

    

    // Initialize the Microsoft Graph client
    const client = Client.initWithMiddleware({
      authProvider: {
        getAccessToken: async () => {
          const token = await credentials.getToken("https://graph.microsoft.com/.default");
          acc_tkn=token;
          console.log("token",token)
          return token.token;
        },
      },
      
      fetchOptions:{
        agent:new HttpProxyAgent('http://cloudproxy.com:8080/')
      }
    });

    
    // Specify the SharePoint site and file path
    const siteId ='ad2********************';
    const filename=file_name
    const filePath =`/PMO_Portal/${p_id}/${folder_name}/${filename}`; // Replace with your desired file path
    const fileContent=filedata
    console.log("filedata",fileContent)
    console.log("client",client)

    
   

  
    
    // Upload the file
    const upload=await client.api(`/sites/${siteId}/drive/root:${filePath}:/content`).put(fileContent,(error)=>{
      console.log("error is as follows",error)
    })
    return upload;
    
  } catch (error) {
    console.log(`Error uploading file: ${error}`);
    return error;
  }


TypeError: fetch failed
    at node:internal/deps/undici/undici:12442:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  statusCode: -1,
  code: 'TypeError',
  requestId: null,
  date: 2024-01-01T12:14:05.978Z,
  body: 'TypeError: fetch failed',
  headers: undefined
}
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,264 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.