API Gatewayのcors有効化をOpen APIで実装する

API Gatewayのcors有効化をOpen APIで実装する

Lambdaプロキシ統合

Open APIのymlファイルにcors設定を追加します。

OPTIONSメソッドを追加します。

    options:
      responses:
        "200":
          description: "200 response"
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: "string"
            Access-Control-Allow-Methods:
              schema:
                type: "string"
            Access-Control-Allow-Headers:
              schema:
                type: "string"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Empty"
      x-amazon-apigateway-integration:
        type: "mock"
        responses:
          default:
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        passthroughBehavior: "when_no_match"

Access-Control-Allow-MethodsはOPTIONS,POSTとしていますが、GETも使用している場合はOPTIONS,POST,GETと追加する必要があります。

コメント

タイトルとURLをコピーしました