Search code examples
djangopython-3.ximagejpeg

Django, Store jpg file received as string in http POST


I am receiving an http request from a desktop application with a screenshot. I cannot speak with the developer or see source code, so all I have is the http request I am getting.

The file isn't in request.FILES, it is in request.POST.

@csrf_exempt
def create_contract_event_handler(request, contract_id, event_type):
    keyboard_events_count = request.POST.get('keyboard_events_count')
    mouse_events_count = request.POST.get('mouse_events_count')
    screenshot_file = request.POST.get('screenshot_file')

    barr2 = bytes(screenshot_file.encode(encoding='utf8'))
    with open('.test/output.jpeg', 'wb') as f:
        f.write(barr2)
        f.close()

The file is corrupted.

The binary starts like this, I don't know if that helps:

����JFIFHH��C



%# , #&')*)-0-(0%()(��C



(((((((((((((((((((((((((((((((((((((((((((((((((((��   `"��

Also, if I try to open the image with PIL, I get the following error:

from PIL import Image
        im = Image.open('./test/output.jpg')
        #OSError: cannot identify image file './test/output.jpg'

Solution

  • Finally, I managed to touch the code in the other hand, the 'filename' was missing in the header and for that reason I was getting the file in the POST instead of in the FILES dictionary.