The process of building custom applications and tools that interact with Microsoft SharePoint, including SharePoint Online in Microsoft 365.
Doubt about MS Graph api
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 365 and Office | SharePoint | Development
Microsoft Security | Microsoft Graph
An API that connects multiple Microsoft services, enabling data access and automation across platforms