Microsoft Graph API Sharepoint upload files gives error as Unable to read JSON request payload

Ankit Kumar 91 Reputation points
2020-12-10T16:18:49.033+00:00

Below is the error and the code I have for uploading files in SharePoint Library. I am trying to upload it in a folder inside a document library. It works fine if I am uploading it in root level of library but gives below error while uploading it in a folder Releae342 inside a library. Any clue what is wrong here? This api do not require any JSON content

Code for upload:

Function GET-UPLOADLINK
{
    param($clientId, $clientSecret, $artifactname)
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadLinkRequestHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadLinkRequestHeaders.Add("Content-Type", "application/json")
    $uploadLinkRequestHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)
    $uploadLinkRequestBody = ""
    $artifactname = "test"
    $fullname = "https://graph.microsoft.com/v1.0/sites/$($SPid)/drives/$($LibId)/root:/Release342/"    
    $comname = $fullname + $artifactname        
    $uploadLinkResponse = Invoke-RestMethod "$($comname).zip/createUploadSession" -Method 'POST' -Headers $uploadLinkRequestHeaders -Body $uploadLinkRequestBody
    $uploadLinkResponse | ConvertTo-Json
    return $uploadLinkResponse    
}

Error Received:

    Invoke-RestMethod : {
2020-12-10T14:41:58.9122308Z   "error": {
2020-12-10T14:41:58.9122643Z     "code": "BadRequest",
2020-12-10T14:41:58.9123035Z     "message": "Empty Payload. JSON content expected.",
2020-12-10T14:41:58.9124293Z     "innerError": {
2020-12-10T14:41:58.9124899Z       "date": "2020-12-10T14:41:58",
2020-12-10T14:41:58.9125415Z       "request-id": "d58a3213-cb8c-4e95-aac2-a076d36f88d9",
2020-12-10T14:41:58.9125886Z       "client-request-id": "d58a3213-cb8c-4e95-aac2-a076d36f88d9"
2020-12-10T14:41:58.9126535Z     }
2020-12-10T14:41:58.9126848Z   }
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,571 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Moroch 1 Reputation point
    2021-01-27T18:09:31.973+00:00

    Any update on this? Having similar problems in python trying to write a sample file to sharepoint

    {'error': {'code': 'BadRequest', 'message': 'Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.', 'innerError': {'date': '2021-01-27T18:08:43', 'request-id': '13db30a4-9ba1-49de-aa4b-ce97d8ea91bf', 'client-request-id': '13db30a4-9ba1-49de-aa4b-ce97d8ea91bf'}}}
    
    
    
    base_url = 'https://graph.microsoft.com/v1.0/'
    path = 'sites/mediamathsp.sharepoint.com/drive/items/mmoroch-test/mmoroch-test-2/test.csv:/content'
    requests.put(base_url + path,headers=headers, data='{"name":"John","age":31,"city":"New York"}').json()    
    
    Out[72]: {'error': {'code': 'invalidRequest', 'message': 'Invalid request', 'innerError': {'date': '2021-01-27T18:31:18', 'request-id': 'f88e9a8e-d8ea-4107-bbfc-83cf35fe3754', 'client-request-id': 'f88e9a8e-d8ea-4107-bbfc-83cf35fe3754'}}}
    
    0 comments No comments

  2. Ankit Kumar 91 Reputation points
    2021-01-27T18:37:15.16+00:00

    Hi @Michael Moroch ,

    Check your Json, My issue was with JSON. here is the solution in detail

    https://stackoverflow.com/questions/65223488/sharepoint-create-folder-with-graph-api-in-powershell

    0 comments No comments