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 réponses
0
Réponse acceptée

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
répondu il y a 9 mois
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
répondu il y a 10 mois
  • 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.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions