来自AWS API Gateway的预检响应中的Access-Control-Allow-Methods不允许使用方法PUT

时间:2017-07-04 21:43:55

标签: amazon-web-services swagger aws-lambda aws-api-gateway

我已经将API网关设置为指向lambda函数,设置为aws_proxy。我可以GET,POST,DELETE就好了,但我尝试添加PUT并获得Method PUT is not allowed by Access-Control-Allow-Methods in preflight response

  

XMLHttpRequest无法加载https://api.small.pictures/picture/07e78691-20f9-4a20-8be5-458eaeb73a63。在预检响应中,Access-Control-Allow-Methods不允许使用方法PUT。

我认为我的CORS设置正确。这是该路线的招摇用户。

  '/picture/{picId}':
    options:
      summary: CORS support
      description: |
        Enable CORS by returning correct headers
      consumes:
        - application/json
      produces:
        - application/json
      tags:
        - CORS
      x-amazon-apigateway-integration:
        type: mock
        requestTemplates:
          application/json: |
            {
              "statusCode" : 200
            }
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'"
              method.response.header.Access-Control-Allow-Methods : "'*'"
              method.response.header.Access-Control-Allow-Origin : "'*'"
            responseTemplates:
              application/json: |
                {}
      parameters:
        - name: picId
          in: path
          required: true
          type: string
      responses:
        200:
          description: Default response for CORS method
          headers:
            Access-Control-Allow-Headers:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Origin:
              type: "string"
    x-amazon-apigateway-any-method:
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-swagger-router-controller: main
      x-lambda-function: ../../swiki/build/picture
      x-amazon-apigateway-integration:
        type: aws_proxy
        httpMethod: POST
        uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/@@LambdaFunctionPicture/invocations
        credentials: @@APIGatewayExecutionRole

如您所见,我已配置Access-Control-Allow-HeadersAccess-Control-Allow-MethodsAccess-Control-Allow-Origin

为什么我无法发出PUT请求?

1 个答案:

答案 0 :(得分:11)

目前,大多数浏览器似乎都不支持在允许的方法上设置'*'。因此,您必须手动设置方法以实现浏览器支持。

print

Access-Control-Allow-Methods

  

兼容性说明

     

中提到的通配符值(*)   最新规范尚未在浏览器中实现:

     

Chromium:Issue 615313

     

Firefox:错误1309358

     

伺服:问题13283