Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,486 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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!