Lambda Function with API Gateway HTTP API: Request Body is NULL

0

Hi,

I am building a Unity3d game which send a POST UnityWebRequest to call a lambda function through its HTTP API trigger. I have enabled CORS by setting Access-Control-Allow-Origin: * in CORS section in the API Gateway after the game deployment. The POST call was able to send a OPTIONS request to the API Gateway. However, the body field is null even though I have json data body attached in the POST call in Unity.

I have tried many suggested solutions such as return Access-Control-Allow-Origin: * in my lambda function response, but still cannot solve it. Would be appreciated if anyone has encountered a similar issue like mine and can share some potential thoughts on how to resolve it.

2 Answers
0
Accepted Answer

After a few more attempts, I realised I only needed to ensure the lambda function to return status 200 to pass the preflight check of the OPTIONS request, then will auto trigger the POST HTTP API call which solved my issue.

Wwwtr
answered 9 months ago
0

Heya, The issue you are facing is likely due to the way that CORS works. When you enable CORS on an API Gateway, you are essentially telling the API Gateway that it should allow requests from certain origins. However, this does not mean that the API Gateway will automatically pass the body of the request to the Lambda function. The Lambda function will only receive the body of the request if the origin of the request is witelisted in the CORS configuration.

To resolve this issue, you need to add the origin of your Unity3D game to the CORS configuration for your API Gateway. You can do this by following these steps:

Go to the API Gateway console. Click the CORS tab for the API Gateway that you are using. Click the Add Origin button. Enter the origin of your Unity3D game in the Origin field. Click the Save button. Once you have added the origin of your Unity3D game to the CORS configuration, the Lambda function should start receiving the body of the request from your game.

Here are some additional things to check:

Make sure that you have enabled CORS on the API Gateway. Make sure that you have added the origin of your Unity3D game to the CORS configuration. Make sure that the body of the request is in the correct format. Make sure that the Lambda function is configured to handle POST requests.

profile picture
answered 9 months ago
  • Thanks for your reply. After a few more attempts, I realised I only needed to ensure the lambda function to return status 200 to pass the preflight check of the OPTIONS request, then will auto trigger the POST HTTP API call which solved my issue.

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