Large File Posting works and the file gets uploaded, but returns 400 error code and states: "POST method cannot be used for media resource" and recommends GET/PUT but but PUT does not allow to upload a new file
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
Can you share more about your query? What kind of file are you uploading, how big is "large", etc. Also need context... SharePoint environment, etc...
The more context details you can provide, the easier it is to help assist on issues. Any code you can provide and/or screenshots of the issue also help. The easier you can make it to reproduce the issue, the easier and quicker it is for someone to help you. Please refer to How to Create Good Issues, specifically How to Create Good Issues: Include context, in our wiki for more details.
For testing I'm uploading a small file (simple json with one key:value pair) for testing. The POST does work, but comes back with an error message.
The sharepoint/directory I'm uploading to is due to it beging crated by teams, so the directory name is something like "Shared Documents/my channel". I did not do anything to enable media types for the sharepoint (as I see is an option in the site settings)
The code looks something like this:
# headers also include bearer token
headers['X-HTTP-METHOD'] = "POST"
headers['Accept'] = "application/json;odata=verbose",
headers['Content-Length'] = str(len(file_contents))
headers['Content-Type'] = 'application/octet-stream'
result = requests.post(
(self.site_url +
f"/_api/web/GetFolderByServerRelativeUrl(" +
f"'{self.site_path}/{sharepoint_folder}')/Files/Add(url='{sharepoint_filename}', overwrite=true)"+
"/$value"),
headers=headers, data=file_contents)
The result comes back as the below _(even though the post works)_
{'error': {'code': '-1, '
'Microsoft.SharePoint.Client.InvalidClientQueryException',
'message': {'lang': 'en-US',
'value': 'The HTTP POST method cannot be used for media '
'resource. The media resource could only be '
'accessed by HTTP GET or updated by HTTP '
'PUT.'}}}
Why do you have headers['X-HTTP-METHOD'] = "POST"? That's not necessary... remove that to see if it helps.
What kind of file are you uploading? What file extension?
I've tried with and without the POST, same failure.
The name of the file being uploaded is "test.json"
changing to test.json.bin did not work
How big is the file? Have you tried uploading it via the browser to ensure you can upload it?
Assuming you have reviewed this:
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-large-files-by-using-rest
Also, the Microsoft Graph REST API provides a useful resumable upload option which works well for large files in SharePoint Online:
https://docs.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
The working with large files documentation is what I reviewed and used as an example here (and where I found the link to provide feedback)
As mentioned: the upload actually works its just that it comes back with error (even though the result is successful)
Can you remove the /$value part of your call and let me know if it resolves the error?
Your API call isn't right. You would only pass the $value when using a Get to retrieve or a PUT request to update a file, but not during the /add endpoint.
This issue has been automatically marked as stale because it has marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within the next 7 days of this comment. Please see our wiki for more information: Issue List Labels: Needs Author Feedback & Issue List: No response from the original issue author
Thank you that seemed to do it. I thought I copy pasted from the docs to do this, but I admit it is not there now, not sure if I mixed it with the example below the big files or if it was updated, but either way, its working now. Thank you!
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues
Most helpful comment
Can you remove the /$value part of your call and let me know if it resolves the error?
Your API call isn't right. You would only pass the $value when using a Get to retrieve or a PUT request to update a file, but not during the /add endpoint.