Could not parse request body into json: Could not parse payload into json: Unrecognized token

0

Few months ago we configure a lambda, to recibe 'application/x-www-form-urlencoded' content type. The Mapping template was working.

But now is not resolving: ""message": "Could not parse request body into json: Could not parse payload into json: Unrecognized token 'testId': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (byte[])"testId=44444"; line: 1, column: 8]""

The Mapping template is:

*`## See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload

#set($allParams = $input.params()) { "data" : "$input.body", "body" : { #foreach( $token in $input.body.split('&') ) #set( $keyVal = $token.split('=') ) #set( $keyValSize = $keyVal.size() ) #if( $keyValSize >= 1 ) #set( $key = $util.urlDecode($keyVal[0]) ) #if( $keyValSize >= 2 ) #set( $val = $util.urlDecode($keyVal[1]) ) #else #set( $val = '' ) #end "$key": "$val"#if($foreach.hasNext),#end #end #end }, "params" : { #foreach($type in $allParams.keySet()) #set($params = $allParams.get($type)) "$type" : { #foreach($paramName in $params.keySet()) "$paramName" : "$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end #end } #if($foreach.hasNext),#end #end }, "context":{ "account-id" : "$context.identity.accountId", "api-id" : "$context.apiId", "api-key" : "$context.identity.apiKey", "authorizer-principal-id" : "$context.authorizer.principalId", "caller" : "$context.identity.caller", "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", "cognito-identity-id" : "$context.identity.cognitoIdentityId", "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", "http-method" : "$context.httpMethod", "stage" : "$context.stage", "source-ip" : "$context.identity.sourceIp", "user" : "$context.identity.user", "user-agent" : "$context.identity.userAgent", "user-arn" : "$context.identity.userArn", "request-id" : "$context.requestId", "resource-id" : "$context.resourceId", "resource-path" : "$context.resourcePath" } }`*

¿ Can we have any idea about what is wrong now ?

1 Answer
0

The issue you're experiencing seems to be related to how the API Gateway is processing the incoming request body. The error message suggests that the API Gateway is attempting to parse the request body as JSON, but it's receiving form-encoded data instead.

Here are a few things to check and potential solutions:

  1. Content-Type Header: Ensure that the client sending the request is setting the Content-Type header to 'application/x-www-form-urlencoded'. If this header is not set correctly, API Gateway might default to trying to parse the body as JSON.

  2. API Gateway Configuration: Double-check your API Gateway configuration to make sure it's set up to handle form-encoded data. In the Integration Request settings, verify that the Content Handling is set to "Convert to Text (if needed)".

  3. Lambda Proxy Integration: If you're using Lambda Proxy integration, the raw body is passed directly to your Lambda function. In this case, you might need to parse the form-encoded data within your Lambda function instead of relying on API Gateway to do the transformation.

  4. Mapping Template: While your mapping template looks correct for handling form-encoded data, ensure it's actually being applied. Check that the Content-Type header in the Method Request matches exactly what you've set for the mapping template (including case sensitivity).

  5. API Gateway Changes: AWS occasionally updates API Gateway, which could potentially affect how certain configurations work. Check if there have been any recent updates or changes to API Gateway that might impact your setup.

  6. Testing: Try testing with a simple form-encoded payload (e.g., "key1=value1&key2=value2") to see if the issue persists. This can help isolate whether the problem is with the specific payload or the overall configuration.

If none of these solve the issue, you might need to contact AWS support for further assistance, as there could be an underlying issue with the API Gateway service or your specific configuration.

Remember to thoroughly test any changes in a non-production environment before applying them to your live API.
Sources
Tutorial: Create a REST API with a Lambda non-proxy integration - Amazon API Gateway
Tutorial: Create a calculator REST API with two AWS service integrations and one Lambda non-proxy integration - Amazon API Gateway

profile picture
answered 7 months ago

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