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 個回答
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
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南