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 

Mistake:

"Multipage form parsing error - Invalid border in multipart: None"

What is the correct way to publish a file? thanks

inquiries. version '2.10.0'

+3
python django-rest-framework python-requests
source share
1 answer

Removed 'content-type' from 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 
+8
source share

All Articles