python - Uploading a file to a Django PUT handler using the requests library -


i have rest put request upload file using django rest framework. whenever uploading file using postman rest client works fine:

postman configuration

but when try code:

import requests api_url = "http://123.316.118.92:8888/api/"  api_token  = "1682b28041de357d81ea81db6a228c823ad52967"  url = api_url + 'configuration/configlet/31' #files = { files = {'file': open('configlet.txt','rb')} print url print "update url ==-------------------" headers = {'content-type' : 'text/plain','authorization':api_token} resp = requests.put(url,files=files,headers = headers) print resp.text print resp.status_code 

i getting error on server side:

multivaluedictkeyerror @ /api/configuration/31/  "'file'" 

i passing file key still getting above error please let me know might doing wrong here.

this how django server view looks

def put(self, request,id,format=none):     configlet = self.get_object(id)     configlet.config_path.delete(save=false)     file_obj = request.files['file']     configlet.config_path = file_obj     file_content = file_obj.read()     params = parse_file(file_content)     configlet.parameters = json.dumps(params)     logger.debug("file content: "+str(file_content))     configlet.save() 

for work need send multipart/form-data body. should not setting content-type of whole request text/plain here; set mime-type of 1 part:

files = {'file': ('configlet.txt', open('configlet.txt','rb'), 'text/plain')} headers = {'authorization': api_token} resp = requests.put(url, files=files, headers=headers) 

this leaves setting content-type header request whole library, , using files sets multipart/form-data you.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -