Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requests: post multipart/form-data

My API:

class FileView(APIView):
    parser_classes = (MultiPartParser,)
    def post(self, request):
        do something with request.FILES.dict().iteritems()

My requests file:

try:
    headers = {
        'content-type': "multipart/form-data",
        'authorization': "Basic ZXNlbnRpcmVcYdddddddddddddd",
        'cache-control': "no-cache",
    }
    myfile = {"file": ("filexxx", open(filepath, "rb"))}

    response = requests.request("POST", verify=False, url=url, data=myfile, headers=headers)

    print(response.text)
except Exception as e:
    print "Exception:", e

Error:

"Multipart form parse error - Invalid boundary in multipart: None"

What is right way to post the file? Thanks

requests.version '2.10.0'

like image 257
BAE Avatar asked Jul 08 '16 15:07

BAE


1 Answers

Removed 'content-type' from the headers, now it works

try:
    headers = {
        'authorization': "Basic ZXNlbnRpcmVcYdddddddddddddd",
        'cache-control': "no-cache",
    }
    myfile = {"file": ("filexxx", open(filepath, "rb"))}

    response = requests.request("POST", verify=False, url=url, data=myfile, headers=headers)

    print(response.text)
except Exception as e:
    print "Exception:", e
like image 65
BAE Avatar answered Oct 22 '22 06:10

BAE