406 Not Acceptable while uploading files

121 views
Skip to first unread message

Kamilla Holanda

unread,
Oct 30, 2020, 9:32:02 PM10/30/20
to Django REST framework
Hey there!

I am working on a file upload functionality and right now I'm stucked on a weird problem. I am always getting the error:

------------------------------------------------------------------------------
{"errors":[{"detail":"Could not satisfy the request Accept header.","status":"406","source":{"pointer":"/data"},"code":"not_acceptable"}]}
------------------------------------------------------------------------------


When I am sending the following request:

------------------------------------------------------------------------------
POST /aposento-fotos/ HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Content-Length: 24364
Accept: application/json,text/javascript
DNT: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvxVpkZTvYVihDGem
Origin: http://localhost:4200
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
------------------------------------------------------------------------------


I am getting the following response:


------------------------------------------------------------------------------
HTTP/1.1 406 Not Acceptable
Date: Sat, 31 Oct 2020 01:18:44 GMT
Server: WSGIServer/0.2 CPython/3.6.9
Content-Type: application/vnd.api+json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 138
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *

------------------------------------------------------------------------------

I have read about the DRF Parsers and tried it all. Nothing is working for my view:

------------------------------------------------------------------------------
class MyUploadViewSet(ModelViewSet):
    queryset = Photos.objects.all()
    serializer_class = PhotosSerializer
    permission_classes = [IsOwnerOrAdminUser]
    parser_classes = [MultiPartParser,
                      FormParser, JSONParser, FileUploadParser]
------------------------------------------------------------------------------


It only works when I use a generic view:


------------------------------------------------------------------------------
class MyUploadView(View):
  def get(self, request, **kwargs):
        print(request)
        print(request.method)
        print(request.GET)

    def post(self, request, **kwargs):
         pass
------------------------------------------------------------------------------


But I want to get it working with the ModelViewSet.

Does anybody know how could I get it working with ModelViewSet? I think it's not working because the request Content-Type, but It was supposed to work with a Parser, right? I am really confused and would appreciate any help.
Thank you in advance.



Wanderley S

unread,
Oct 30, 2020, 9:35:12 PM10/30/20
to django-res...@googlegroups.com
Hi there. I'm not quite sure if this will solve the issue, but vou should be handling the upload as a post request.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/b29de415-1908-40fa-b831-9080cb956c8bn%40googlegroups.com.

Kamilla Holanda

unread,
Oct 30, 2020, 9:42:41 PM10/30/20
to Django REST framework
@Wandss Thank you for your answer! 

I am not sure If I understood what you mean. My client is an EmberJS app and it fires a post request to upload the file and I think that the ViewSet action to deal with a post request is the "create" action, right? Should I deal with it using other action?

Wanderley S

unread,
Oct 30, 2020, 9:50:20 PM10/30/20
to django-res...@googlegroups.com
In your code I just saw this part:


class MyUploadView(View):
  def get(self, request, **kwargs):
        print(request)
        print(request.method)
        print(request.GET)

    def post(self, request, **kwargs):
         pass

Did you try this?


class PhotoAlbumCreateAPIView(CreateAPIView):
    serializer_class = PhotoAlbumSerializer
    queryset = PhotoAlbum.objects.all()


Kamilla Holanda

unread,
Oct 30, 2020, 10:00:03 PM10/30/20
to Django REST framework
Just tried and now I'm getting this error:

AttributeError: type object 'AposentoFotoViewSet' has no attribute 'get_extra_actions'


I have changed the code to:

-------------------------------------------------------------------
from rest_framework.generics import CreateAPIView

class PhotoAlbumViewSet(CreateAPIView):
    queryset = PhotoAlbum.objects.all()
    serializer_class = PhotoAlbumSerializer

    def post(self, request, *args, **kwargs):
        print("CREATE Photos")
-------------------------------------------------------------------

Is that the right package?

Kamilla Holanda

unread,
Oct 30, 2020, 10:12:27 PM10/30/20
to Django REST framework
I have changed the url to:

    path('photos/', PhotoAlbumViewSet.as_view(), name='photos'),

And it solved the error that I mentioned above.

But now I'm still getting the 406 Not Acceptable error:


{"errors":[{"detail":"Could not satisfy the request Accept header.","status":"406","source":{"pointer":"/data"},"code":"not_acceptable"}]}

Wanderley S

unread,
Oct 30, 2020, 10:56:56 PM10/30/20
to django-res...@googlegroups.com
According to the error details, seems that your hearder is not correct at client side. 
You can try to use the browseable API from DRF for testing. After that I suggest you to review your front end.
Are you using axios for handling the API  requests?

Kamilla Holanda

unread,
Nov 1, 2020, 1:45:59 PM11/1/20
to Django REST framework
I wasn't able to get the ModelViewset working for uploading files so I changed my approach and used one dedicated endpoint to upload files and kept the other models as regular models. Not sure if it's the best approach but I need to move fast on these features.

Thank you for your help!

Reply all
Reply to author
Forward
0 new messages