python POST a StringIO using poster -


here situation:

i use pil process image, save stringio object. now, want post stringio object through poster. but, can't image in request.files dict. have googled few hours, found question, python : post data within stringio through poster? tried don't work.

so, read poster source code , found try 'name' attribution of file-like object param, seems stringio object doesn't have 'name' attribution, so, filename , filetype none

if hasattr(value, 'read'):     # looks file object     filename = getattr(value, 'name', none)     if filename not none:         filetype = mimetypes.guess_type(filename)[0]     else:         filetype = none      retval.append(cls(name=name, filename=filename,         filetype=filetype, fileobj=value)) else:     retval.append(cls(name, value)) 

so, specify name attribution of stringio object , seems work fine.

im_tmp = image.open(stringio(bits)) //bits: binary chars of image im_res = imageapi.process(im_tmp, mode, width, height) //imageapi: class use pil methods process image output = stringio() im_res.save(output, format='png') output.name = 'tmp.png' //i add above code , works call(url_str=url, file_dict={'file':output}) //call: package of poster 

did right? what's right way post stringio object through poster?

according this commit, making name optional done explicitly support passing in stringio object, discovered skips detecting mime type , defaults text/plain instead.

your approach then, entirely correct. set .name attribute goad poster detecting mime type.

the alternative use better library post web. recommend @ requests, supports multipart posting of files out of box, including way set filename. mimetype based on explicit filename if passed in.


Comments