Django rest api redirect to S3 download link returns mime type warning in chrome console





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using Django Rest Framework to build an API and I'm trying to redirect my endpoint to download files from AWS S3. It's working but in Chrome I'm getting the following warning:




Resource interpreted as Document but transferred with MIME type
application/force-download




This is my get method from my view:



def get(self, request, pk, format=None):
file_item = self.get_object(pk)
serializer = FileSerializer(file_item)

response = redirect(serializer.data['file_url'])
return response


As you can see, I have passed content type, why does it Chrome still throw a warning?
I need to be able to download files in various formats e.g. pdf, jpg etc.



Update: I have specified the mime type correctly and when I print the response object, this is what I get. Is it the "test/html" header that is causing issues? How do I remove it?



<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="https://a.com/xxxx/x.pdf&response-content-type=application/pdf&response-content-disposition=attachment%3Bfilename%3D%22xxx.pdf%22">









share|improve this question

























  • Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

    – Jonah Bishop
    Nov 24 '18 at 12:50











  • Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

    – kbsol
    Nov 24 '18 at 13:32











  • I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

    – Jonah Bishop
    Nov 24 '18 at 13:51











  • Thanks Jonah for your comments, I have updated the code above, still having issues.

    – kbsol
    Nov 24 '18 at 14:52


















0















I'm using Django Rest Framework to build an API and I'm trying to redirect my endpoint to download files from AWS S3. It's working but in Chrome I'm getting the following warning:




Resource interpreted as Document but transferred with MIME type
application/force-download




This is my get method from my view:



def get(self, request, pk, format=None):
file_item = self.get_object(pk)
serializer = FileSerializer(file_item)

response = redirect(serializer.data['file_url'])
return response


As you can see, I have passed content type, why does it Chrome still throw a warning?
I need to be able to download files in various formats e.g. pdf, jpg etc.



Update: I have specified the mime type correctly and when I print the response object, this is what I get. Is it the "test/html" header that is causing issues? How do I remove it?



<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="https://a.com/xxxx/x.pdf&response-content-type=application/pdf&response-content-disposition=attachment%3Bfilename%3D%22xxx.pdf%22">









share|improve this question

























  • Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

    – Jonah Bishop
    Nov 24 '18 at 12:50











  • Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

    – kbsol
    Nov 24 '18 at 13:32











  • I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

    – Jonah Bishop
    Nov 24 '18 at 13:51











  • Thanks Jonah for your comments, I have updated the code above, still having issues.

    – kbsol
    Nov 24 '18 at 14:52














0












0








0








I'm using Django Rest Framework to build an API and I'm trying to redirect my endpoint to download files from AWS S3. It's working but in Chrome I'm getting the following warning:




Resource interpreted as Document but transferred with MIME type
application/force-download




This is my get method from my view:



def get(self, request, pk, format=None):
file_item = self.get_object(pk)
serializer = FileSerializer(file_item)

response = redirect(serializer.data['file_url'])
return response


As you can see, I have passed content type, why does it Chrome still throw a warning?
I need to be able to download files in various formats e.g. pdf, jpg etc.



Update: I have specified the mime type correctly and when I print the response object, this is what I get. Is it the "test/html" header that is causing issues? How do I remove it?



<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="https://a.com/xxxx/x.pdf&response-content-type=application/pdf&response-content-disposition=attachment%3Bfilename%3D%22xxx.pdf%22">









share|improve this question
















I'm using Django Rest Framework to build an API and I'm trying to redirect my endpoint to download files from AWS S3. It's working but in Chrome I'm getting the following warning:




Resource interpreted as Document but transferred with MIME type
application/force-download




This is my get method from my view:



def get(self, request, pk, format=None):
file_item = self.get_object(pk)
serializer = FileSerializer(file_item)

response = redirect(serializer.data['file_url'])
return response


As you can see, I have passed content type, why does it Chrome still throw a warning?
I need to be able to download files in various formats e.g. pdf, jpg etc.



Update: I have specified the mime type correctly and when I print the response object, this is what I get. Is it the "test/html" header that is causing issues? How do I remove it?



<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="https://a.com/xxxx/x.pdf&response-content-type=application/pdf&response-content-disposition=attachment%3Bfilename%3D%22xxx.pdf%22">






python django google-chrome django-rest-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:52







kbsol

















asked Nov 24 '18 at 12:26









kbsolkbsol

1079




1079













  • Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

    – Jonah Bishop
    Nov 24 '18 at 12:50











  • Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

    – kbsol
    Nov 24 '18 at 13:32











  • I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

    – Jonah Bishop
    Nov 24 '18 at 13:51











  • Thanks Jonah for your comments, I have updated the code above, still having issues.

    – kbsol
    Nov 24 '18 at 14:52



















  • Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

    – Jonah Bishop
    Nov 24 '18 at 12:50











  • Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

    – kbsol
    Nov 24 '18 at 13:32











  • I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

    – Jonah Bishop
    Nov 24 '18 at 13:51











  • Thanks Jonah for your comments, I have updated the code above, still having issues.

    – kbsol
    Nov 24 '18 at 14:52

















Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

– Jonah Bishop
Nov 24 '18 at 12:50





Possible duplicate of Utility of HTTP header "Content-Type: application/force-download" for mobile?

– Jonah Bishop
Nov 24 '18 at 12:50













Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

– kbsol
Nov 24 '18 at 13:32





Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else.

– kbsol
Nov 24 '18 at 13:32













I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

– Jonah Bishop
Nov 24 '18 at 13:51





I believe the root issue is that you need to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc.

– Jonah Bishop
Nov 24 '18 at 13:51













Thanks Jonah for your comments, I have updated the code above, still having issues.

– kbsol
Nov 24 '18 at 14:52





Thanks Jonah for your comments, I have updated the code above, still having issues.

– kbsol
Nov 24 '18 at 14:52












2 Answers
2






active

oldest

votes


















0














I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:



response = HttpResponse(data, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response


data should be the file data, and filename the name of the file.



See this question for more.






share|improve this answer

































    0














    I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458126%2fdjango-rest-api-redirect-to-s3-download-link-returns-mime-type-warning-in-chrome%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:



      response = HttpResponse(data, content_type='application/pdf')
      response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
      return response


      data should be the file data, and filename the name of the file.



      See this question for more.






      share|improve this answer






























        0














        I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:



        response = HttpResponse(data, content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
        return response


        data should be the file data, and filename the name of the file.



        See this question for more.






        share|improve this answer




























          0












          0








          0







          I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:



          response = HttpResponse(data, content_type='application/pdf')
          response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
          return response


          data should be the file data, and filename the name of the file.



          See this question for more.






          share|improve this answer















          I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:



          response = HttpResponse(data, content_type='application/pdf')
          response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
          return response


          data should be the file data, and filename the name of the file.



          See this question for more.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 15:04

























          answered Nov 24 '18 at 14:58









          Jonah BishopJonah Bishop

          9,07433357




          9,07433357

























              0














              I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.






              share|improve this answer




























                0














                I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.






                share|improve this answer


























                  0












                  0








                  0







                  I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.






                  share|improve this answer













                  I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 15:07









                  kbsolkbsol

                  1079




                  1079






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458126%2fdjango-rest-api-redirect-to-s3-download-link-returns-mime-type-warning-in-chrome%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings