5
3

More than 3 years have passed since last update.

AWS API GatewayのHTTP API(β版)がLambda関数に渡す値が想定と違っていた話

Posted at

最近 AWSのAPI Gatewayに、HTTP API(β版)が実装された。
試してみたところ、ちょっとハマったので備忘録に。

<html>
  <head></head>
  <body>
   <form method="post" action="xxxxxxxx">
     <input name="userName" value="a">
     <input name="userPass" value="a">
     <button>
   </form>
  </body>
</html>

まずは、ありきたりなフォームを作成して、作成したHTTP APIにsubmitする。
統合先のLambda関数で確認すると、bodyの情報がREST API(Lambdaプロキシ統合を使用)のときと違う。

// Lambda関数(Node.js 12.x)
exports.handler = async event => {
  console.log(event.body);
  // REST API(Lambdaプロキシ統合使用)の場合は 'userName=a&userPass=a'
  // HTML API(β版)の場合は 'dXNlck5hbWU9YSZ1c2VyUGFzcz1h'
};

どうやらBase64でエンコードされているらしい。
isBase64Encoded をデコードの判定に使おう。

// Lambda関数(Node.js 12.x)
exports.handler = async event => {
  const bodyString = event.isBase64Encoded ? Buffer.from(event.body, 'base64').toString() : event.body;
  console.log(bodyString);
  // REST API(Lambdaプロキシ統合使用)の場合は 'userName=a&userPass=a'
  // HTML API(β版) の場合も 'userName=a&userPass=a'
};

良かった。
解決できた。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3