<p>I installed octoprint locally to help debug this problem.</p>

<ol>
<li>Your code snippet says "def def Upload_File" which is a syntax error.</li>
<li>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 <a href="https://stackoverflow.com/questions/17415084/multipart-data-post-using-python-requests-no-multipart-boundary-was-found">this StackOverflow</a> question, which states to remove the custom content-type header to fix this problem.</li>
<li>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. </li>
</ol>

<p>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.</p>

<pre><code>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()
</code></pre>

<p>Finally, if you're going to be doing a whole lot of REST API shenanigans with your Octoprint server, <a href="https://github.com/dougbrion/OctoRest" rel="nofollow noreferrer">may I offer a prebuilt library?</a></p>

CC BY-SA 4.0
2019-06-02T22:17:34.230Z
Not sure how much the REST API has changed in the last several updates, but I'd assume it has and that library hasn't been updated in a year.
Chase Cromwell
CC BY-SA 4.0
2019-06-03T00:38:14.093Z
I actually used that library to fix the code snippet above lol. There may be issues with it, but generally speaking REST APIs tend to be largely the same - from a development standpoint it's almost always better to simply add new functionality rather than changing old functionality, because it breaks less downstream code. The only time I'd go against that is if there's a fundamental problem with the old stuff and it needs to be totally overhauled.
Nach0z
CC BY-SA 4.0
2019-06-03T08:25:44.527Z
Removing the  custom Content-Type header seems to working .now everything works smoothly Thank you ..!
white_ghost
CC BY-SA 4.0
2019-06-01T15:53:47.960Z
2019-06-01T15:53:47.960Z
Nach0z
<p>I'm trying to upload a file using Octoprint REST API - it seems to be possible as per the document described here, <a href="http://docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder" rel="nofollow noreferrer">Upload file or create folder</a>, but I can't figure it out how to do it with the Python request lib.</p>

<p>Currently what I'm doing is </p>

<pre><code> 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()
</code></pre>

<p>The response returns status is 400</p>

<p>My working env is:</p>

<ul>
<li>Windows 10 and Octoprint running on virtual environment.</li>
</ul>

CC BY-SA 4.0
2019-06-01T16:23:05.730Z
FWIW the user posted this identical question on Octoprint's community.
300D7309EF17
<octoprint>
Uploading files using Octoprint REST API
white_ghost
# Uploading files using Octoprint REST API

(Asked by: username_0 on 2019-06-01)

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.

**Tags:** octoprint
> **Comments:**
> **username_1**: FWIW the user posted this identical question on Octoprint's community.  

---

## 1 answers

### Answer by username_2 on 2019-06-01. Score: 4 | Accepted answer

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?

> **Comments:**
> **username_3**: Not sure how much the REST API has changed in the last several updates, but I'd assume it has and that library hasn't been updated in a year.  
> **username_2**: I actually used that library to fix the code snippet above lol. There may be issues with it, but generally speaking REST APIs tend to be largely the same - from a development standpoint it's almost always better to simply add new functionality rather than changing old functionality, because it breaks less downstream code. The only time I'd go against that is if there's a fundamental problem with the old stuff and it needs to be totally overhauled.  
> **username_0**: Removing the  custom Content-Type header seems to working .now everything works smoothly Thank you ..!  

---

