# Support file uploads also for HTTP PUT method

**URL:** <https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826>\
**Category:** Forms & APIs\
**Created:** [May 9, 2023, 12:17pm UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826 "2023-05-09T12:17:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oto-stefan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/oto-stefan/32/13633_2.png) [@oto-stefan](https://forum.djangoproject.com/u/oto-stefan)\
**Post date:** [May 9, 2023, 12:17pm UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/1 "2023-05-09T12:17:15Z")

</div>

Hi, everybody!

Based on the [documentation](https://docs.djangoproject.com/en/4.2/ref/request-response/#django.http.HttpRequest.FILES) the out-of-the-box support for file uploads is only available for the HTTP POST method. I suggest this is extended also for the PUT method.

Reasons: When building a REST API interface one uses the PUT method for modification/updating resources. The “resource” might include some data as well as a file. While file uploads with the PUT method are generally possible the direct support in Django is missing and this might complicate building a REST interface in a consistent way.

My understanding is this could hardly break some existing functionality and the implementation would not be that complicated.

Regards,  
Oto

---

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [May 9, 2023, 12:40pm UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/2 "2023-05-09T12:40:19Z")

</div>

G’day Oto,

This has been requested before, see the comments in this ticket plus the linked mailing list threads: [#12635 (Process HTTP PUT into request.FILES and request.PUT as done for POST) – Django](https://code.djangoproject.com/ticket/12635)

David

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [May 9, 2023, 1:03pm UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/3 "2023-05-09T13:03:15Z")

</div>

+1.

There’s also though a GSoC project to address [#21442 (Configurable request parsing.) – Django](https://code.djangoproject.com/ticket/21442) this year. That should allow a parsed `request.data` for all request methods. (So if configured parsers include multipart file parsing, that this should well be supported at that point.)

---

<div class="post-metadata">

**Author:** ![oto-stefan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/oto-stefan/32/13633_2.png) [@oto-stefan](https://forum.djangoproject.com/u/oto-stefan)\
**Post date:** [May 10, 2023, 5:25am UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/4 "2023-05-10T05:25:04Z")

</div>

Hi, thanks for responding.

It looks I should have been more careful searching the forum not to duplicate topics, sorry for that. While I get the reasons for refusing the suggestion in the past I do not completely agree. I still believe that sending binary data with the `multipart/form-data` encoding is an efficient way of doing so, HTTP and web are not about browsers only and web browsers themselves can do (and really do so) lots of their requests with the PUT method (to REST services, e.g.) using JavaScript.

Anyway, thanks once more for your attention,  
Oto

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [May 10, 2023, 5:27am UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/5 "2023-05-10T05:27:03Z")

</div>

Hopefully the GSoC project should open the gate here. 🤞

---

<div class="post-metadata">

**Author:** ![DhavalGojiya](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dhavalgojiya/32/26014_2.png) [@DhavalGojiya](https://forum.djangoproject.com/u/DhavalGojiya)\
**Post date:** [December 31, 2024, 11:26am UTC](https://forum.djangoproject.com/t/support-file-uploads-also-for-http-put-method/20826/6 "2024-12-31T11:26:18Z")

</div>

```auto
    def patch(self, request, *args, **kwargs):
        """
        Handle Django HTTP file upload in PATCH/PUT request manually.

        By default, Django file uploads are only supported in the 'POST' HTTP method via `request.FILES`.
        For other HTTP methods, `request.FILES` will return an empty MultiValueDict: <MultiValueDict: {}>
        """
        from django.http.multipartparser import MultiPartParser

        text_data, file_data = MultiPartParser(
            request.META, request, request.upload_handlers
        ).parse()

        # The parse() method returns a tuple with 2 elements:
        # `text_data` contains HTTP text data, and `file_data` contains HTTP file data.

        profile_picture = file_data.get("profile_picture") # My image field name
        # type(text_data) ---> <class 'django.http.request.QueryDict'>
        # type(file_data) ---> <class 'django.utils.datastructures.MultiValueDict'>

        user_obj = User.objects.create(
            first_name="Christopher", last_name="Nolan", profile_picture=profile_picture
        )

        print(user_obj.profile_picture.path) # Image stored location
        # Output: `/home/dhaval/interstellar/media/profile-pictures/nolan_dp.jpg`

        return HttpResponse(
            b"Mankind was born on Earth. It was never meant to die here."
        )

```
