Decode Complex MultiPart/Form-Data in Lambda Function using Python

0

I am trying to decode the following object structure, which contains text data and file data in Python.

groupId: text
groupName: text
friendlyName: text
shortDescription: text
mainDescriptionList: [ {descriptionTitle: text, descriptionText: text } ]
imagesList: [ { imageTitle: text, file: File } ]
coordinatorList: [ { cordinatorName: text, file: File } ]

The following is how the console outputs the outgoing request to the lambda function.

groupId: 
groupName: AQQQ QWEJLER LELERLNE
friedlyName: 
shortDescription: Amdslkfn dlsfjs dsfkjdgsmd dsfjdsfsdf dljsdojjwe;'fgmfg v;ojdsf dsnfsdlkg;ldsm;sdjs/msdfjgfsd;.
mainDescriptionList[]: [object Object]
mainDescriptionList[]: [object Object]
imagesList[]: [object Object]
imagesList[]: [object Object]
eventList[]: [object Object]
eventList[]: [object Object]
coordinatorList[]: [object Object]
coordinatorList[]: [object Object]

In the Lambda function, I have written the following code to decode the incoming multipart/form-data from my ReactJS application. In the code I'm outputting the header and the content into CloudWatch log.

def lambda_handler(event, context):
    
    headers = event['headers']
    postData = base64.b64decode(event['body'])
    
    for part in decoder.MultipartDecoder(postData, headers['content-type']).parts:
        print(part.headers[b'Content-Disposition'].decode('utf-8'))
        print(part.text)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

In the CloudWatch log it shows the following output;

form-data; name="groupName"
AQQQ QWEJLER LELERLNE

form-data; name="shortDescription"
Amdslkfn dlsfjs dsfkjdgsmd dsfjdsfsdf dljsdojjwe;'fgmfg v;ojdsf dsnfsdlkg;ldsm;sdjs/msdfjgfsd;.

form-data; name="mainDescriptionList[]"
[object Object]

form-data; name="mainDescriptionList[]"
[object Object]

form-data; name="imagesList[]"
[object Object]

form-data; name="imagesList[]"
[object Object]

form-data; name="eventList[]"
[object Object]

form-data; name="eventList[]"
[object Object]

form-data; name="coordinatorList[]"
[object Object]

form-data; name="coordinatorList[]"
[object Object]

I want to extract/decode the object type data, which contains in, mainDescriptionList, imagesList, eventList & coordinatorList. But struggling how I can achieve that in Python (v3.12).

Appreciate your help on this.

Update: I am using "requests_toolbelt" package in python for decoding the multipart/form-data. And the data is sent from ReactJs application as a 'content-type': 'multipart/form-data' request to the lambda function.

champer
asked 2 months ago73 views
2 Answers
0

Might be worth converting the body into JSON and extracting the required data

import json

def lambda_handler(event, context):
    headers = event['headers']
    body_str = event['body']

    try:
        body_json = json.loads(body_str)

        # Extract the required values from the JSON
        main_description_list = body_json.get('mainDescriptionList', [])
        coordinator_list = body_json.get('coordinatorList', [])

        # Process the extracted values as needed
        print(f"Main Description List: {main_description_list}")
        print(f"Images List: {images_list}")
        print(f"Event List: {event_list}")
        print(f"Coordinator List: {coordinator_list}")

        # Additional processing or logic can be added here

    except (json.JSONDecodeError, KeyError):
        print("Error: Request body is not valid JSON or missing required keys")

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
profile pictureAWS
answered 2 months ago
0

Thanks @hnsoni-from-aws. I have tried your solutions but it throws the exception for JSONDecodeError. I have missed to mention that the ReactJS application send the request to the lambda function with the "' content-type': 'multipart/form-data' " in its header. Since it contains files (basically images files for "imagesList" and "coordinatorList").

I am not sure why it is this hard to process an request that contains files or am I missing something or is there a different way to do this?

champer
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions