我正在创建一个带有VPC集成的AWS API网关,该集成可以代理另一个内部服务 . 这是一个POST请求 . 代理服务在UI中按预期响应,该UI直接使用附加的正确标头调用服务 .

有趣的是,当我使用AWS Amplify调用该服务时,预检检查和POST成功,并且响应体看起来很完美 . 但我从Amplify(通过axios库)获得通用"Network Error" . Chrome会将此错误记录到控制台: Failed to load https://my-amazon-api-url/thing: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. I 'm unsure as to why that header is not in the response and I can'似乎找到了一种方法来获取响应 . 注意:代理服务返回带有响应正文的201 . 我认为这可能与它有关,但我不确定 .

Preflight Headers

响应 Headers :

accept-encoding: gzip, deflate, br
access-control-allow-credentials: true
access-control-allow-headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Access-Control-Allow-Origin
access-control-allow-methods: POST,OPTIONS
access-control-allow-origin: *
access-control-expose-headers: Access-Control-Allow-Origin
content-length: 0
content-type: application/json
date: Wed, 20 Jun 2018 17:30:10 GMT
status: 200
x-amz-apigw-id: someId
x-amzn-requestid: someID

POST Headers

请求 Headers :

:authority: someAmazonUrl
:method: POST
:path: /thing
:scheme: https
accept: application/json, text/plain, */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
access-control-allow-origin: *
authorization: AWS4-HMAC-SHA256 Credential=someCredentials, SignedHeaders=access-control-allow-origin;content-type;host;x-amz-date;x-amz-security-token, Signature=aSignature
cache-control: no-cache
content-length: 559
content-type: application/json
dnt: 1
origin: http://localhost:8080
pragma: no-cache
referer: http://localhost:8080/
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
x-amz-date: 20180620T173010Z
x-amz-security-token: aReallyLongToken

REPONSE Headers :

content-encoding: gzip
content-length: 463
content-type: application/json; charset=utf-8
date: Wed, 20 Jun 2018 17:30:10 GMT
status: 201
x-amz-apigw-id: agatewayId
x-amzn-remapped-connection: keep-alive
x-amzn-remapped-content-length: 866
x-amzn-remapped-date: Wed, 20 Jun 2018 17:30:10 GMT
x-amzn-requestid: aLongId

Swagger JSON for the specific route

"/thing": {
      "post": {
        "operationId": "PostThing",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "Access-Control-Allow-Origin",
            "in": "header",
            "required": false,
            "type": "string"
          },
          {
            "name": "Content-Type",
            "in": "header",
            "required": false,
            "type": "string"
          },
          {
            "name": "Accept-Encoding",
            "in": "header",
            "required": false,
            "type": "string"
          },
          {
            "in": "body",
            "name": "ThingPostBody",
            "required": true,
            "schema": {
              "$ref": "#/definitions/ThingPostBody"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "Access-Control-Allow-Origin": {
                "type": "string"
              }
            }
          },
          "201": {
            "description": "201 response",
            "headers": {
              "Access-Control-Allow-Origin": {
                "type": "string"
              },
              "Content-Encoding": {
                "type": "string"
              },
              "Accept-Encoding": {
                "type": "string"
              },
              "Content-Type": {
                "type": "string"
              }
            }
          },
          "400": {
            "description": "400 response",
            "headers": {
              "Access-Control-Allow-Origin": {
                "type": "string"
              }
            }
          },
          "401": {
            "description": "401 response",
            "headers": {
              "Access-Control-Allow-Origin": {
                "type": "string"
              }
            }
          },
          "403": {
            "description": "403 response",
            "headers": {
              "Access-Control-Allow-Origin": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "uri": "http://internal-amazon-url/thing",
          "responses": {
            "default": {
              "statusCode": "201",
              "responseParameters": {
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              }
            }
          },
          "requestParameters": {
            "integration.request.header.Access-Control-Allow-Origin": "'*'"
          },
          "passthroughBehavior": "when_no_match",
          "connectionType": "VPC_LINK",
          "connectionId": "someID",
          "httpMethod": "POST",
          "type": "http_proxy"
        }
      },
      "options": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "headers": {
              "Access-Control-Expose-Headers": {
                "type": "string"
              },
              "Access-Control-Allow-Origin": {
                "type": "string"
              },
              "Access-Control-Allow-Methods": {
                "type": "string"
              },
              "Accept-Encoding": {
                "type": "string"
              },
              "Access-Control-Allow-Headers": {
                "type": "string"
              },
              "Content-Type": {
                "type": "string"
              }
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "responses": {
            "2\\d{2}": {
              "statusCode": "200",
              "responseParameters": {
                "method.response.header.Access-Control-Expose-Headers": "'Access-Control-Allow-Origin,Content-Type,Content-Encoding'",
                "method.response.header.Access-Control-Allow-Methods": "'POST,OPTIONS'",
                "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Access-Control-Allow-Origin'",
                "method.response.header.Access-Control-Allow-Origin": "'*'",
                "method.response.header.Accept-Encoding": "'gzip, deflate, br'"
              }
            }
          },
          "requestTemplates": {
            "application/json": "{\"statusCode\": 200}"
          },
          "passthroughBehavior": "when_no_templates",
          "type": "mock"
        }
      }
    }