API error while testing in postman

0

I have a web application form that allows user to upload image, enter watermark text, color and size. So when the form is submitted an API call should be initiated to trigger Lamda function to watermark the uploaded image with the other input data. I tried to test this on Postman and in the configuration I selected Content-type as multipart/form-data as Header and added image file from my local machine and other parameters in the payload. But now I am getting this error: ` "message": "Could not parse request body into json: Could not parse payload into json: Unrecognized character escape ''' (code 39)\n at [Source: (byte[])"{\n "body": "----------------------------613288133030835038548709\r\nContent-Disposition: form-data; name=\"image-file\"; filename=\"airbnb-logo.jpeg\"\r\nContent-Type: image\/jpeg\r\n\r\n\�\�\�\�\

And my lambda function is `import boto3 import json from PIL import Image, ImageDraw, ImageFont import io import base64

s3 = boto3.client('s3') bucket = "inputimgsbucket"

def lambda_handler(event, context): # Parse the request body body = json.loads(event['body'])

# Retrieve the watermark text, color, and image file
watermark_text = body['watermark-text']
watermark_color = body['watermark-color']
watermark_size = int(body['watermark-size'])

# Read the image data from the file
image_data = base64.b64decode(image_file['body'])`
1 Answer
0

The error message you're seeing is due to the fact that the AWS Lambda function is trying to parse the entire HTTP request body as JSON, but it's actually multipart/form-data. Multipart/form-data is primarily used for uploading files and it packages the data in a different way than a typical JSON payload.

In your case, your Lambda function is expecting a JSON payload, but it's getting multipart/form-data, which includes the binary data of the file and other form data. This is why you're seeing the parsing error.

To solve this, you need to handle the parsing of the multipart/form-data within your Lambda function. AWS Lambda does not natively support multipart/form-data. You need to manually parse the multipart/form-data.

profile picture
answered 10 months ago
  • any document or reference to guide towards it? Like an example?

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