Doubt about MS Graph api

Uziel Alonso 0 Reputation points
2024-11-13T18:59:41.3666667+00:00

Hello I am working on uploading files to sharepoint online using the ms graph rest api, but I encounter some issues. I am working with python as my programming language and using the requests library to do the calls. I am following this documentation https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http to that, using this piece of code

with open("file.xlsx", "rb") as file:
        file_content = file.read()
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "text/plain"
    }
    response = requests.put(url=f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/items/{parent_folder_id}:/{file}:/content", headers=headers, data=file_content)
    print(response.status_code)
    print(json.dumps(response.json(), indent=2))


And getting the following error

{
  "error": {
    "code": "BadRequest",
    "message": "Entity only allows writes with a JSON Content-Type header.",
    "innerError": {
      "date": "2024-11-13T18:42:27",
      "request-id": "a72b83a6-dcb6-46de-93de-8f0374cd1669",
      "client-request-id": "a72b83a6-dcb6-46de-93de-8f0374cd1669"
    }
  }
}



As stated in the documentation, the content type should be text/plain, but it is asking for JSON.

Any help would be much appreciated.

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,486 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,118 questions
0 comments No comments
{count} votes

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.