PUT request to upload a file not working in Flask [duplicate]
Asked Answered
O

1

7

I am working on a web application using Flask. One of the views is supposed to accept uploaded files through PUT requests, however I only can get POST requests with $ curl -F upload=@filename URL to work properly. With PUT requests such as $ curl --upload-file filenname URL the request.files ImmutableMultiDict is empty. Am I missing something in Flask or maybe with using curl?

Override answered 2/3, 2012 at 12:41 Comment(1)
I don't think this question should have been marked as a duplicate. This question is specifically about using HTTP PUT with Flask to upload files. The linked question is about getting data from HTTP POST/GET requests. Neither the linked question nor any of its current answers talks about HTTP PUT. Not sure if there's a process for getting it unmarked as a duplicate.Princessprinceton
M
8

PUT request is way different compared to POST request. With PUT request the file contents can be accessed using either request.data or request.stream. The first one stores incoming data as string, while request.stream acts more like a file object, making it more suitable for binary data:

with open('uploaded_image.jpg', 'w') as f:
    f.write(request.stream.read())
Manet answered 2/3, 2012 at 13:8 Comment(1)
Yes my file ended up in request.data because Flask cannot handle its mime type.Override

© 2022 - 2024 — McMap. All rights reserved.