NodeJS Code
Lambda@Edge로 배포할때 Viewer Request로 배포합니다.
더보기
// index.mjs (ES Module)
export const handler = async (event, context, callback) => {
var request = event.Records[0].cf.request;
console.log(JSON.stringify(request.body));
// body data를 Base64로 디코딩
let body = Buffer.from(request.body.data, 'base64').toString('utf8');
let parsedBody = JSON.parse(body);
// 'hello'가 있으면 'length'로 변경
if (parsedBody.hasOwnProperty('hello')) {
parsedBody.length = parsedBody.hello;
delete parsedBody.hello;
// 수정된 데이터를 다시 Base64로 인코딩
request.body.action = 'replace';
request.body.data = Buffer.from(JSON.stringify(parsedBody), 'utf8').toString('base64');
}
console.log("request:", JSON.stringify(request, null, 2));
// callback을 사용하여 응답 반환
callback(null, request);
};
DOMAIN=""
HELLO=$(shuf -i 1000-7000 -n 1)
response_code=$(curl -s -w "%{http_code}" -X POST "$DOMAIN/v1/token" \
-H "Content-Type: application/json" \
-d '{"hello":'"$HELLO"'}')
echo "Response Code: $response_code"