python - Requests toolbelt multipart uploading file with CSRF fails -


i trying upload .ipa file drf backend 3rd party app using python multipart data encoder (http://toolbelt.readthedocs.io/en/latest/uploading-data.html). however, getting following error-

('connection aborted.', brokenpipeerror(32, 'broken pipe'))

if remove 'rb' attribute, following error instead-

'utf-8' codec can't decode byte 0xe3 in position 10: invalid continuation byte

could please point out what's wrong here? btw, decided use requests toolbelt since might upload huge files.

from django.views.generic import view  django.conf import settings import os import requests #sudo pip install requests, it's external library requests_toolbelt.multipart.encoder import multipartencoder #pip install requests-toolbelt    class upload_binary(generics.genericapiview):  def post(self, request, format=none):      url = "http://localhost:9020/"     csrf = requests.get(url).cookies['csrftoken']      post_url = "http://localhost:9020/upload/"     upload_file_name = "someapp.ipa"     media_dir = settings.media_root     upload_file_path = os.path.join(media_dir, upload_file_name)       filedata = multipartencoder(fields = {         'csrfmiddlewaretoken': csrf, 'file1': ('file', open(upload_file_path, 'rb'))     })      headersdict = {'x-csrftoken': csrf, 'content-type': filedata.content_type}     upload_bin_req = requests.post(post_url, data = filedata,  headers = headersdict)      return jsonresponse({}) 

answering question myself, in case people need know. apparently, csrf on 3rd party app causing issue. had update 3rd party code allow rest access me disabling csrf view in question. had import csrf_exempt , decorate view in question following-

    django.views.decorators.csrf import csrf_exempt      ...     ...    @csrf_exempt     def the_third_party_view_i_was_calling(request): 

in case 1 knows better solution fix without disabling csrf, please let me know.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -