I want to POST form data to api gateway which then sends it to lambda for processing. However, I get
{
"message": "Could not parse request body into json: Unexpected character (\'-\' (code 45))
in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n
at [Source: (byte[])\"----------------------------086228525798973846089611\r\nContent-
Disposition: form-data; name=\"first_name\"\r\n\r\nfake\r\n---------------------------
-086228525798973846089611\r\nContent-Disposition: form-data;
name=\"last_name\"\r\n\r\nname\r\n---------------------------
-086228525798973846089611\r\nContent-Disposition: form-data;
name=\"email\"\r\n\r\nfakename1#something.com\r\n---------------------------
-086228525798973846089611\r\nContent-Disposition: form-data; name=\"mobile\"\r\n\r\n\r\n-
---------------------------086228525798973\"[truncated 752 bytes]; line: 1, column: 3]"
}
In the cloudwatch logs I can see lambda gives this error: Lambda invocation failed with status: 400. Can't see any errors in lambda logs just api gateway logs.
My api gateway POST method looks like this:
Edit:
Adding image of form I am trying to submit that the API isn't liking:
Any ideas how to POST through to lambda without api gateway transformations or other issue?
Related
I have a POST method which integrates with SNS. It initially accepted a TopicArn query string parameter and a Message, however to avoid client applications having knowledge of the ARN, and to move the message content into the request body, I needed to make use of an API Gateway mapping template.
I added the below Velocity (VTL) code under an application/json mapping template (fake account ID):
TopicArn=$util.urlEncode('arn:aws:sns:eu-west-2:1234567890123:MyTopic')
&Subject=$util.urlEncode('Contact form message')
&Message=$util.urlEncode($input.body)
I ran a test using the console with a body of:
{
"name": "test",
"email": "test#test.com",
"message": "test message"
}
but this returns the error:
Invalid parameter: TopicArn or TargetArn Reason: no value for required parameter
Endpoint request body after transformations: TopicArn=arn%3Aaws%3Asns%3Aeu-west-2%1234567890123%3AMyTopic
&Subject=Contact+form+message
&Message=%7B%0A++++%22name%22%3A+%22test%22%2C%0A++++%22email%22%3A+%22test%40test.com%22%2C%0A++++%22message%22%3A+%22test+message%22%0A%7D
Sending request to https://sns.eu-west-2.amazonaws.com/?Action=Publish
Received response. Status: 400
I'm not particularly familiar with VTL, but I literally copied some example syntax from AWS docs yet the TopicArn parameter doesn't appear to be passed to SNS.
Can anyone see what I've done wrong here?
I've got an API Gateway in front of a Lambda.
Successful responses from the Lambda (HTTP 2xx) have the response body forwarded.
However, for error responses (HTTP 5xx and others), the API Gateway transforms the response body using response templates.
Is there a way to avoid this? To have the original error response body from the Lambda?
In my Lambda I have this:
return callback(generalError, {
statusCode: 500,
headers:{
"content-type": "application/json"
},
body: JSON.stringify({
error: 'INTERNAL_ERROR',
description: error.message,
})
});
However, as output from the Gateway I get this:
{ "error": "Internal server error" }
Which doesn't match. The Lambdas response. It does match the response template in API Gateway:
{"message":$context.error.messageString}
However, is there a way to just proxy the original Lambda response instead of having this transformation in place?
I've found the reason why it doesn't work.
If you set a callback to a 500 error, including an object with an error field, somehow this will become the full response, regardless of the other output or real error.
Avoiding using an error field does the trick! I renamed it to serviceError and now I'm getting the responses I expected.
I have my API Gateway set up with application/json Content-Type mapped to
{
"param1": "$input.params('param1')"
}
and application/pdf mapped to
{
"content": "$input.body"
}
Both of these work on their own when I use postman and specify the Content-Type as one or the other.
But, I want to be able to make a request that can take both pdf binary data along with some parameters from JSON using one call and use both in lambda.
I've tried adding both of these in a content-type and setting the template as
{
"content": "$input.body",
"param1": "$input.params('param1')"
}
But this returns back an error when I call it with the params and the pdf:
"message": "Could not parse request body into json: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n
The "param1" is a query string parameter I have added.
I am trying to test POST method in my API gateway and i am getting error:
{"message": "Could not parse request body into json: Unexpected
character (\'}\' (code 125)): expected a value\n at [Source:
[B#11a9cb82; line: 5, column: 2]"}
the request body i am using:
"{\"DataTypes\":\"ADL\"}"
I guess you should not escape anything.
If you try with
{"DataTypes": "ADL"}
it should work.
There is an API-Gateway - SNS integration.
Request body is application/json
Integration parameters:
Subject: method.request.body.Subject
Message: method.request.body.Message
TopicArn: 'arn:aws:sns:eu-west-1:*******:some-topic'
HTTP Headers: Content-Type='application/json'
Content handling : passthrough
Request body example:
{
"Subject": "Hello World",
"Message": "qwerty"
}
Now I'm struggling to make it pass new line characters (\n, \r\n, "\r\n" and many others combinations) inside the message to SNS. Value of 'Message' key is getting passed "as is".
Is there something I'm missing?