Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 578
af_khan Avatar asked Aug 03 '26 09:08

af_khan


1 Answers

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'}];
like image 169
lmX2015 Avatar answered Aug 06 '26 09:08

lmX2015



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!