graph api file upload responds bad request

Rajarajacholan 0 Reputation points
2024-01-10T13:16:11.56+00:00

I am trying to upload a file to shared documents folder and the code snippet below, it seems the url is not correct, can someone help fixing it?

const apiEndPoint = '/Drives/b!rYDqajE65kKOoUdGp97Z3DU9rf0hlVJKqdrGFBaR8HDPFsK6v5gFT76yfU3SRWvV/root';

response:

{
  '@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)/$entity',
  id: '01LJU4E3F6Y2GOVW7725BZO354PWSELRRZ',
  name: 'root',
  webUrl: 'https://rapventures.sharepoint.com/Shared%20Documents'
}

creating large file upload using api

https://graph.microsoft.com/v1.0/sites/root/drive/items/01LJU4E3F6Y2GOVW7725BZO354PWSELRRZ/${fileName}/createUploadSession

response:

status code: 400, code: invalidRequest
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Md Asif Muztaba 325 Reputation points Microsoft External Staff
    2024-01-11T01:31:16.0466667+00:00

    The error code 400 typically indicates a bad request. This could be due to an incorrect URL or missing parameters. In your case, it seems like the URL for creating the upload session might be incorrect. The URL should point to the specific item in the drive where you want to upload the file. Here’s the correct format for the URL:

    POST /drives/{driveId}/items/{itemId}/createUploadSession
    POST /groups/{groupId}/drive/items/{itemId}/createUploadSession
    POST /me/drive/items/{itemId}/createUploadSession
    POST /me/drive/items/{parentItemId}:/{fileName}:/createUploadSession
    POST /sites/{siteId}/drive/items/{itemId}/createUploadSession
    POST /users/{userId}/drive/items/{itemId}/createUploadSession
    
    

    You need to replace {site-id}, {item-id}, and {fileName} with the actual values. {site-id} is the ID of the site that contains the drive. {item-id} is the ID of the drive item under which you want to upload the file. {fileName} is the name of the file you want to upload.
    For further information please check this documentation. Please try using this format and let me know if you still encounter issues. I’m here to help!

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

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.