canvasapi
canvasapi copied to clipboard
BadRequest exception while uploading file to user folder
Describe the bug
when I call the Folder.upload
method, the BadRequest
exception always raised.
To Reproduce
Steps to reproduce the behavior:
- Get my user by
Canvas.get_current_user
- Filter and get the desired folder by
user.get_folders()
- Run
folder.upload("filename.txt")
. -
BadRequest
is raised.
Expected behavior
I wish the file in my local system to be successfully uploaded to the user folder on canvas.
Environment information
- Python version: Python 3.9.2
- CanvasAPI version : 2.1.0
Additional context
The codes are similar as following:
API_URL = ...
API_KEY = ...
canvas = Canvas(API_URL, API_KEY)
user = canvas.get_current_user()
root = 'my files'
target_folder = next(filter(lambda f: f.full_name == root folder.get_folders())))
# this raises the exception
root.upload('1.txt')
and this raises
---------------------------------------------------------------------------
BadRequest Traceback (most recent call last)
<ipython-input-27-f9f690c59d0c> in <module>
----> 1 f.upload('1.txt', name='1.txt')
~/.local/lib/python3.9/site-packages/canvasapi/folder.py in upload(self, file, **kwargs)
134 """
135 my_path = "folders/{}/files".format(self.id)
--> 136 return Uploader(self._requester, my_path, file, **kwargs).start()
~/.local/lib/python3.9/site-packages/canvasapi/upload.py in start(self)
61 if self._using_filename:
62 with open(self.file, "rb") as file:
---> 63 return self.request_upload_token(file)
64 else:
65 return self.request_upload_token(self.file)
~/.local/lib/python3.9/site-packages/canvasapi/upload.py in request_upload_token(self, file)
47 )
48
---> 49 return self.upload(response, file)
50
51 def start(self):
~/.local/lib/python3.9/site-packages/canvasapi/upload.py in upload(self, response, file)
85 kwargs = response.get("upload_params")
86
---> 87 response = self._requester.request(
88 "POST",
89 use_auth=False,
~/.local/lib/python3.9/site-packages/canvasapi/requester.py in request(self, method, endpoint, headers, use_auth, _url, _kwargs, json, **kwargs)
235 # Raise for status codes
236 if response.status_code == 400:
--> 237 raise BadRequest(response.text)
238 elif response.status_code == 401:
239 if "WWW-Authenticate" in response.headers:
BadRequest:
Is the way I called the function wrong? Thanks in advance.