我正在使用具有代理到另一个内部服务的VPC集成来创建AWS API网关。这是一个POST请求。代理服务在UI中按预期方式响应,该UI使用附加的正确标头直接调用该服务。
有趣的是,当我使用AWS Amplify调用服务时,预检检查和POST成功完成了,并且响应正文看起来很完美。但是我从Amplify(通过axios库)得到一个通用的“网络错误”。 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.
我不确定为什么该标头不在响应中,而且我似乎找不到找到在响应中获取标头的方法。注意:代理服务返回带有响应正文的201。我认为可能与它有关,但是我不确定。
Preflight标头
响应标题:
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标头
请求标头:
: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
响应标题:
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
为特定路由放大JSON
"/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"
}
}
}