Parse multipart request string in Python

前端 未结 5 1609
走了就别回头了
走了就别回头了 2020-12-10 09:15

I have a string like this

\"--5b34210d81fb44c5a0fdc1a1e5ce42c3\\r\\nContent-Disposition: form-data; name=\\\"author\\\"\\r\\n\\r\\nJohn Smith\\r\\n--5b34210d         


        
5条回答
  •  無奈伤痛
    2020-12-10 10:18

    Had a bunch of weird encoding issues and also odd behavior with api gateway, originally received the body of the request at bytes and then after redeploying started to receive them as base64. Anyway this is the code that ended up working for me.

    import json
    import base64
    import boto3
    from requests_toolbelt.multipart import decoder
    
    s3client = boto3.client("s3")
    def lambda_handler(event, context):
        content_type_header = event['headers']['content-type']
        postdata = base64.b64decode(event['body']).decode('iso-8859-1')
        imgInput = ''
        lst = []
        for part in decoder.MultipartDecoder(postdata.encode('utf-8'), content_type_header).parts:
            lst.append(part.text)
        response = s3client.put_object(  Body=lst[0].encode('iso-8859-1'),  Bucket='test',    Key='mypicturefinal.jpg')
        return {'statusCode': '200','body': 'Success', 'headers': { 'Content-Type': 'text/html' }}
    

提交回复
热议问题