AWS Java Lambda not reading request body

I am trying to create a simple aws lambda in Java. When I test the lambda from AWS console, it works fine but when I run the same request body from Postman, the object values are null. The java code is

public class UserRegistration implements RequestHandler<UserRegistrationRequest, UserRegistrationResponse> {

public UserRegistrationResponse handleRequest(final UserRegistrationRequest input, final Context context) {

    LambdaLogger logger = context.getLogger();
    logger.log("*********************UserRegistrationRequest: " + input);

    headers.put("X-Custom-Header", "application/json");
    UserRegistrationResponse response = new UserRegistrationResponse();
  try {
        System.out.println("----------- Event body " + input);

        response.setBody("{SuccessFully Registered}");
        response.setStatusCode(200);
        return response;
    } catch (Exception e) {
        response.setBody("{}");
        response.setStatusCode(500);
        return response;
    }
}

UserRegistrationRequest is

@Data
public class UserRegistrationRequest {
    private String fullname;
    private String email;
    private String mobile;
    private String password;
    private String tnc;
}

The request Body is

{   
        "fullname": "ABC",
        "email": "[email protected]",
        "mobile": "0611234123234",
        "password": "something",
        "tnc": "yes"
    }

When I run the code from AWS console, the response is

----------- Event body UserRegistrationRequest(fullname=ABC, [email protected], mobile=0611234123234, password=something, tnc=yes)

The same request body when run from the Postman gives response

----------- Event body UserRegistrationRequest(fullname=null, email=null, mobile=null, password=null, tnc=null)

I am using the function URL to invoke the function. If you have faced similar situation, could you please guide me. Thanks.

  • Are you using a REST API Gateway in front of the Lambda? If that is the case, you should set the Integration Request type to LAMBDA_PROXY.

    – 

  • @AndresBores No I am not using REST api gateway. I am using the Function URL of the lambda.

    – 

  • Are you aware that the payload format is the same as the API Gateway payload format v2? docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html. So, what you receive is a JSON like this {"headers": {}, "requestContext": {}, "body": ""}. Of course, there are many more attributes.

    – 




Leave a Comment