Uploading files using Octoprint REST API I'm trying to upload a file using Octoprint REST API - it seems to be possible as per the document described here, Upload file or create folder, but I can't figure it out how to do it with the Python request lib.

Currently what I'm doing is 

 import requests

 def def Upload_File():
       fle={'location':"j:/max.gcode"}
       url='http://localhost:5000/api/files/{}'.format('local')
       payload={'select': 'true','print': 'false' }
       header={'content-type': 'multipart/form-data','X-Api-Key': 'FD550BD4DA2442BA906AD1850539D6DB' }
       response = requests.post(url, files=fle,data=payload,headers=header)
       print(response)

if __name__=='__main__':
    Upload_File()


The response returns status is 400

My working env is:


Windows 10 and Octoprint running on virtual environment.
I installed octoprint locally to help debug this problem.


Your code snippet says "def def Upload_File" which is a syntax error.
If you go into Octoprint, under Settings -> Logging, and download octoprint.log, you'll notice it says "WARNING - 400 POST /api/files/local (::1): No multipart boundary supplied". A quick google search led me to this StackOverflow question, which states to remove the custom content-type header to fix this problem.
You actually need to open() the file, and assign that value to the 'file' field in the fle object definition, not just tell it where it's currently at on your machine. Additionally, you need to provide a filename field in the fle object. 


I got your code snippet working as below, went ahead and swapped your API key back into it. You'll need to point the open() call to the actual location of your file on your hard drive, which I assume is j:/max.gcode, rather than mine which is just opening max.gcode from the running directory of the script.

import requests 

def Upload_File():
   fle={'file': open('max.gcode', 'rb'), 'filename': 'max.gcode'}
   url='http://localhost:5000/api/files/{}'.format('local')
   payload={'select': 'true','print': 'false' }
   header={'X-Api-Key': 'FD550BD4DA2442BA906AD1850539D6DB' }
   response = requests.post(url, files=fle,data=payload,headers=header)
   print(response)

if __name__=='__main__':
   Upload_File()


Finally, if you're going to be doing a whole lot of REST API shenanigans with your Octoprint server, may I offer a prebuilt library?