AnsweredAssumed Answered

Help to solve the error : Cannot upload a blob after already uploading via url

Question asked by Javier Machin on Jan 29, 2019
Latest reply on Jan 30, 2019 by Kona Jones

Hi, I need the help of the community to understand this error: 

      "<Response [500]> Cannot upload a blob after already uploading via url".

 

The link that I am following as reference: Submission Comments - Canvas LMS REST API Documentation 

 

That happened when I tried to upload a file as comment to the canvas.


import
requests
import os

def upload_file(self, url):
    with open(url, 'rb') as f:
        file_content = f.read()
    return file_content
def upload_jn_to_canvas(self):
   # Step 1 - tell Canvas you want to upload a file
   post_api_url = '/api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id/comments/files'

   session = requests.Session()
   session.headers = {'Authorization': 'Bearer {0}'.format(self.token)}
   response = session.post(post_api_url,
                        data={
                            'name': name_file,
                            'size': os.path.getsize(jupyter_notebook_file),
                            'parent_folder_path': '/',
                            'content-type': 'binary/octet-stream'
                        })

   response.raise_for_status()
   print('Session Headers: ', session.headers)
   print('Post URL: ', post_api_url)
   print(response.json())

   # Step 2 - upload file
   parameters = list(response.json()['upload_params'].items())
   parameters.append((u'file', self.upload_file(jupyter_notebook_file)))

   response2 = requests.post(response.json()['upload_url'], files=parameters)
   response2.raise_for_status()
   print(response2.json())
   print(response2)

Outcomes