ERROR Validation error: Lambda function result failed validation, the function tried to delete read-only header, headerName : Content-Length - amazon-cloudfront

whenever I am trying to add response headers, the CloudFront throws me the
ERROR Validation error: Lambda function result failed validation, the function tried to delete read-only header, headerName : Content-Length.
ERROR Validation error: Lambda function result failed validation, the function tried to delete read-only header, headerName : Content-Encoding.
const response = {
status: '302',
statusDescription: 'Found',
headers: {
'location': [{
key: 'location',
value: 'https://abc.test.io'
}],
'set-cookie': [{
key: 'set-cookie',
value: 'sessiontoken='+sessionObjectData.session.sessionId+'; Secure; HttpOnly'
}]
}
}
callback(null, response)
Can someone let me know what I am doing wrong here ? BTW, I am using viewer-response event

As mentioned on CloudFront functions docs, it is not possible to modify some of the response headers from Edge Functions (including "Content-Length"). What you can try instead is to update only headers you need to change and left others untouched:
const response = event.Records[0].cf.response;
response.status = 302;
response.statusDescription = 'Found';
response.headers['location'] = [{ key: 'Location', value:'https://abc.test.io'}];

Related

JSON not being parsed for validation when doing a POST request to Fastify

In my routes, I have the following:
const reservationSchema = {
body: {
type: 'object',
required: ['in', 'out', 'guests', 'language', 'roomsSelected'],
properties: {
language: {
type: 'string',
},
// ... several other property validations here
}
}
};
fastify.post(
'/api/reservations/:slug',
{ schema: reservationSchema },
reservationsController.addReservation
);
I send the POST request from React like this:
const response = await fetch(process.env.REACT_APP_API_HOSTNAME + '/api/reservations/' + property.slug, {
method: 'POST',
body: JSON.stringify(requestBody)
});
I can see that it is correctly sending JSON when I look at the request:
However I receive the following response:
{
"statusCode":400,
"error":"Bad Request",
"message":"body should be object"
}
Am I missing something to automatically parse the POST body as an object in Fastify so I can validate it using a validation schema? Even in my reservationsController.addReservation() function I need to manually do JSON.parse() on req.body.
From the fetch() docs:
Both request and response (and by extension the fetch() function), will try to intelligently determine the content type. A request will also automatically set a Content-Type header if none is set in the dictionary.
However, (at least in Chrome), when you send a JSON string, it does not intelligently determine that this string is JSON, and instead sends the Content-Type header as text/plain;charset=UTF-8. Since the server receives this Content-Type header, it assumes that you are sending a plan text string and therefore does not parse it as JSON.
To make the server automatically parse the body as JSON, you need to make sure to set the Content-Type header to application/json. Like this:
const response = await fetch(process.env.REACT_APP_API_HOSTNAME + '/api/reservations/' + property.slug, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
});

Dynamo DB query fails through axios with Value null at 'tableName' failed to satisfy constraint

I am making a query to DynamoDB via API Gateway and from Postman the request succeeds with 200 OK, however through Axios (in a VueJS client) the code (as exported from Postman) fails with this error:
{"__type":"com.amazon.coral.validate#ValidationException","message":"1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null"}
Here is the failing Axios code, which Postman generated (only slightly modified to hide the URL):
let url = 'local/api/prospect'
var data = JSON.stringify({
"TableName":"my-table",
"ExpressionAttributeNames":{"#lang":"LANGUAGE"},
"ExpressionAttributeValues":{
":m":{"S":"Acet"},
":c":{"S":"CH"},
":l":{"S":"DE"}},
"FilterExpression":"begins_with(MAT_DESC, :m) and COUNTRY = :c and #lang = :l"
});
var config = {
method: 'post',
url: url,
headers: { 'Content-Type': 'application/json' },
data: data
}
this.$axios(config)
Is there something Postman is doing internally which could cause this request to be successful but which the generated NodeJS request does not include?
UPDATE
I do not know why this works, but this code appears to have solved the problem...
let url = 'local/api/prospect'
var data = JSON.stringify({
TableName: 'my-table',
ExpressionAttributeNames: {'#lang':'LANGUAGE'},
ExpressionAttributeValues:{
':m':{S:'Acet'},
':c':{S:'CH'},
':l':{S:'DE'}},
FilterExpression: 'begins_with(MAT_DESC, :m) and COUNTRY = :c and #lang = :l'
})
var config = {
method: 'post',
url: url,
headers: { 'Content-Type': 'application/json' },
data: data
}
If anyone can tell me, categorically, why this is, please answer below.
This one came down to the formatting of the request. Here is the correct way to format the data:
var data = JSON.stringify({
TableName: 'prospect-materials',
ExpressionAttributeNames: {'#lang':'LANGUAGE'},
ExpressionAttributeValues:{
':m':{S:'Acet'},
':c':{S:'CH'},
':l':{S:'DE'}},
FilterExpression: 'begins_with(MAT_DESC, :m) and COUNTRY = :c and #lang = :l'
})

Request with request-promise and multipart/form-data

I have to implement the following workflow:
Make request no.1, get a JSON response. Manipulate this JSON object so that it can be used as payload in the second request. Make second request.
The first request is successfully implemented. I get the correct JSON object. The manipulation part is also done and tested.
I managed to create a correct postman collection for the second request. It uses the method POST, has several headers, Content-type is multipart/form-data and the payload from request no.1 is sent as Raw (in the body, options none, form-data etc ... I choose raw).
I am trying to implement this second request but get an error no matter what I try to tweak. The following is my code:
const manipulatedObject = await this._flattenPayload(payload);
const Request = require(request-promise);
const options = {
method: 'POST',
uri: 'https://second_request_url',
formData: {
file: {
value: manipulatedObject,
},
},
headers: {
Accept: 'application/json, text/plain, */*',
Connection: 'keep-alive',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxxxxxxxxx', // this is generated dynamically and matches the value in the manipulatedObject
cookie: this.cachedCookie,
'X-XSRF-TOKEN': this.cachedToken,
},
};
Request(options)
.then((body) => {
return body;
})
.catch((error) => {
return error;
});
* The parameter payload is the response from the first request.
The error I get is this:
message=source.on is not a function, stack=TypeError: source.on is not
a function
The variable manipulatedObject is a string. Currently I am copying it's value from the successful postman request to avoid errors from the manipulation process. The random token in the Content-Type header matches the ones in the manipulatedObject string. So the data are identical to the ones I use in the postman request.
I suspect the error is in the way I send the data, but I am having trouble figuring it out. I have read about this error and it seems to be generated when an object is passed to formData, but I am sending a string so I don't understand why it happens.
The values of formData accepts only three types of elements viz. string, Buffer and Stream. Refer to:request/issues/2366
U may change formData as follows:
formData: {
file: manipulatedObject,
},
or
formData: {
file: {
value: manipulatedObject,
options: {},
},
},

401 unauthorized Authentication parameters missing in dialogflow api

I have developed chatbots using Dialogflow api v1. There had been no change in the code. But, now the response from the server is "code":401,"errorType":"unauthorized","errorDetails":"Authentication parameters missing. I checked in Postman using the below url.
https://api.dialogflow.com/v1/query?v=519794&lang=en&query=hi&sessionId=519794&Content-Type=application/json and gave my client access token in bearer token. When I tried with post method I got error.But, when i used GET method it worked in postman. So, I tried the same code in my javascript code and got 401 unauthorized error. What's wrong here?
I have resolved this issue using the following code.
Used GET method instead of POST in Ajax call and removed JSON.stringify in the data parameter.
var text = "hello";
var iRandom = Math.floor((Math.random() * 10000000) + 1);
$.ajax({
type: "GET",
url: "https://api.api.ai/v1/query?v=324233",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer "+accessToken
},
data: { query: text, lang: "en", sessionId: iRandom},
success: function(data) {
//desired action
},
error: function() {
console.log("Internal Server Error");
}
});
Another alternate solution:
I retained the earlier version.
var iRandom = Math.floor((Math.random() * 10000000) + 1);
type: POST,
data: JSON.stringify({ query: text, lang: "en", sessionId: iRandom}),
Instead of passing the iRandom as number, I passed it as String and I was able to get response from the dialogflow.

AWS lambda api gateway error "Malformed Lambda proxy response"

I am trying to set up a hello world example with AWS lambda and serving it through api gateway. I clicked the "Create a Lambda Function", which set up the api gatway and selected the Blank Function option. I added the lambda function found on AWS gateway getting started guide:
exports.handler = function(event, context, callback) {
callback(null, {"Hello":"World"}); // SUCCESS with message
};
The issue is that when I make a GET request to it, it's returning back a 502 response { "message": "Internal server error" }. And the logs say "Execution failed due to configuration error: Malformed Lambda proxy response".
Usually, when you see Malformed Lambda proxy response, it means your response from your Lambda function doesn't match the format API Gateway is expecting, like this
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
If you are not using Lambda proxy integration, you can login to API Gateway console and uncheck the Lambda proxy integration checkbox.
Also, if you are seeing intermittent Malformed Lambda proxy response, it might mean the request to your Lambda function has been throttled by Lambda, and you need to request a concurrent execution limit increase on the Lambda function.
If lambda is used as a proxy then the response format should be
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
Note : The body should be stringified
Yeah so I think this is because you're not actually returning a proper http response there which is why you're getting the error.
personally I use a set of functions like so:
module.exports = {
success: (result) => {
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify(result),
}
},
internalServerError: (msg) => {
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({
statusCode: 500,
error: 'Internal Server Error',
internalError: JSON.stringify(msg),
}),
}
}
} // add more responses here.
Then you simply do:
var responder = require('responder')
// some code
callback(null, responder.success({ message: 'hello world'}))
For Python3:
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({
'success': True
}),
"isBase64Encoded": False
}
Note the body isn't required to be set, it can just be empty:
'body': ''
I had this issue, which originated from an invalid handler code which looks completely fine:
exports.handler = (event, context) => {
return {
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
};
}
I got the hint from examining the somewhat confusing API Gateway response logs:
> Endpoint response body before transformations: null
The way to fix it would be to either
Add the async keyword (async function implicitly returns a Promise):
exports.handler = async (event, context) => {
return {
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
};
}
Return a Promise:
exports.handler = (event, context) => {
return new Promise((resolve) => resolve({
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
}));
}
Use the callback:
exports.handler = (event, context, callback) => {
callback({
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
});
}
My handler was previously declared async without ever using await, so I removed the async keyword to reduce complexity of the code, without realizing that Lambda expects either using async/await/Promise or callback return method.
From the AWS docs
In a Lambda function in Node.js, To return a successful response, call
callback(null, {"statusCode": 200, "body": "results"}). To throw an
exception, call callback(new Error('internal server error')). For a
client-side error, e.g., a required parameter is missing, you can call
callback(null, {"statusCode": 400, "body": "Missing parameters of
..."}) to return the error without throwing an exception.
Just a piece of code for .net core and C# :
using Amazon.Lambda.APIGatewayEvents;
...
var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonConvert.SerializeObject(new { msg = "Welcome to Belarus! :)" }),
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
return response;
Response from lambda will be :
{"statusCode":200,"headers":{"Content-Type":"application/json"},"multiValueHeaders":null,"body":"{\"msg\":\"Welcome to Belarus! :)\"}","isBase64Encoded":false}
Response from api gateway will be :
{"msg":"Welcome to Belarus! :)"}
I've tried all of above suggestion but it doesn't work while body value is not String
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
success: true
}),
isBase64Encoded: false
};
A very very special case, if you pass the headers directly there is a chance you have this header:
"set-cookie": [ "........" ]
But Amazon needs this:
"set-cookie": "[ \\"........\\" ]"
For anyone else who struggles when the response appears valid. This does not work:
callback(null,JSON.stringify( {
isBase64Encoded: false,
statusCode: 200,
headers: { 'headerName': 'headerValue' },
body: 'hello world'
})
but this does:
callback(null,JSON.stringify( {
'isBase64Encoded': false,
'statusCode': 200,
'headers': { 'headerName': 'headerValue' },
'body': 'hello world'
})
Also, it appears that no extra keys are allowed to be present on the response object.
If you're using Go with https://github.com/aws/aws-lambda-go, you have to use events.APIGatewayProxyResponse.
func hello(ctx context.Context, event ImageEditorEvent) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
IsBase64Encoded: false,
StatusCode: 200,
Headers: headers,
Body: body,
}, nil
}
I had this error because I accidentally removed the variable ServerlessExpressLambdaFunctionName from the CloudFormation AWS::Serverless::Api resource. The context here is https://github.com/awslabs/aws-serverless-express "Run serverless applications and REST APIs using your existing Node.js application framework, on top of AWS Lambda and Amazon API Gateway"
Most likely your returning body is in JSON format, but only STRING format is allowed for Lambda proxy integration with API Gateway.
So wrap your old response body with JSON.stringify().
In case the above doesn't work for anyone, I ran into this error despite setting the response variable correctly.
I was making a call to an RDS database in my function. It turned out that what was causing the problem was the security group rules (inbound) on that database.
You'll probably want to restrict the IP addresses that can access the API, but if you want to get it working quick / dirty to test out if that change fixes it you can set it to accept all like so (you can also set the range on the ports to accept all ports too, but I didn't do that in this example):
A common cause of the "Malformed Lambda proxy response" error is headers that are not {String: String, ...} key/values pairs.
Since set-cookie headers can and do appear in multiples, they are represented
in http.request.callback.response as the set-cookie key having an Array of
Strings value instead of a single String. While this works for developers, AWS
API Gateway doesn't understand it and throws a "Malformed Lambda proxy response"
error.
My solution is to do something like this:
function createHeaders(headers) {
const singleValueHeaders = {}
const multiValueHeaders = {}
Object.entries(headers).forEach(([key, value]) => {
const targetHeaders = Array.isArray(value) ? multiValueHeaders : singleValueHeaders
Object.assign(targetHeaders, { [key]: value })
})
return {
headers: singleValueHeaders,
multiValueHeaders,
}
}
var output = {
...{
"statusCode": response.statusCode,
"body": responseString
},
...createHeaders(response.headers)
}
Note that the ... above does not mean Yada Yada Yada. It's the ES6 spread operator.
Here's another approach. Configure the mapping template in your API gateway integration request and response. Go to IntegrationRequest -> MappingTemplate -> select "When there are no templates defined" -> type application/json for content-type. Then you don't have to explicitly send a json. Even the response you get at your client can be a plain string.
The format of your function response is the source of this error. For API Gateway to handle a Lambda function's response, the response must be JSON in this format:
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
Here's an example function in Node.js with the response correctly formatted:
exports.handler = (event, context, callback) => {
var responseBody = {
"key3": "value3",
"key2": "value2",
"key1": "value1"
};
var response = {
"statusCode": 200,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
callback(null, response);
};
Ref: https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/
Python 3.7
Before
{
"isBase64Encoded": False,
"statusCode": response.status_code,
"headers": {
"Content-Type": "application/json",
},
"body": response.json()
}
After
{
"isBase64Encoded": False,
"statusCode": response.status_code,
"headers": {
"Content-Type": "application/json",
},
"body": str(response.json()) //body must be of string type
}
If you're just new to AWS and just want your URL working,
If you haven't created a trigger for your Lambda Function, navigate to the function in Lambda Functions app and create trigger choosing API Gateway.
Navigate to API Gateway App -> Choose your Particular Lambda's API Gateway (Method execution) -> Click on INTEGRATION Request -> Uncheck "Use Lambda Proxy integration" (check box).
Then click on "<-Method Execution" & click on Test Client section. Provide the options and click test button. You should see a success response.
If you are still unable to get a success response, create an alias for the correct version (if you have multiple versions in the Lambda Function)
Pick the URL from the logs and use your POST/GET Tool (Postman) and choose authentication as AWS Signature - provide your authentication keys(AccessKey & SecretKey) in the postman request with AWS Region & Service Name as lambda.
P.S : This may only help beginners and may be irrelevant to others.

Resources