[英]AWS API gateway error while making OPTIONS request
我正在使用 CFT 为我的 API 创建环境。我已经为 CORS 添加了选项。我注意到当我从AWS console for OPTIONS
进行测试时,我得到了200
响应。 但是,当我从CURL or PostMan
执行相同操作时,我收到500
内部服务器错误。 在审查了与之相关的SO问题之后。 我已将集成响应修改为 CONVERT_TO_TEXT。 但这也没有解决问题。
我注意到日志中有一个有线行为。 以下是来自 AWS 控制台的请求的日志片段:
Sat Apr 13 15:06:26 UTC 2019 : Method request headers: { Access-Control-Request-Method= POST, Content-Type= application/json}
Sat Apr 13 15:06:26 UTC 2019 : Method request body before transformations:
Sat Apr 13 15:06:26 UTC 2019 : Method response body after transformations:
Sat Apr 13 15:06:26 UTC 2019 : Method response headers: {X-Requested-With=*, Access-Control-Allow-Headers=Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-requested-with, Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=POST,OPTIONS, Content-Type=application/json}
Sat Apr 13 15:06:26 UTC 2019 : Successfully completed execution
Sat Apr 13 15:06:26 UTC 2019 : Method completed with status: 200
但是当我从 CRUL 或 PM 发出同样的请求时,我看到了以下日志:
Method request path: {}
Method request query string: {}
Method request headers: Method request headers: {Accept=*/*, CloudFront-Viewer-Country=IN, CloudFront-Forwarded-Proto=https, CloudFront-Is-Tablet-Viewer=false, CloudFront-Is-Mobile-Viewer=false, User-Agent=curl/7.55.1, X-Forwarded-Proto=https, CloudFront-Is-SmartTV-Viewer=false, Host=MYHOST, X-Forwarded-Port=443, (CloudFront), Access-Control-Request-Method=POST, CloudFront-Is-Desktop-Viewer=true, Content-Type=application/json}
Method request body before transformations: [Binary Data]
Execution failed due to configuration error: Unable to transform request
Method completed with status: 500
我们可以看到它正在尝试转换[Binary Data]
但我没有发送任何东西。
Curl 我用过: curl -X OPTIONS -H "Access-Control-Request-Headers: Content-Type" -H "Access-Control-Request-Method: POST" -H "Access-Control-Allow-Origin: '*'" -v MYHOST
为什么我在日志中看到这种差异? 我的配置出了什么问题? 你能帮助我吗。
更新:我正在使用下面的 CFT
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
Type: MOCK
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
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-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
method.response.header.Access-Control-Allow-Origin: "'*'"
RequestTemplates:
application/json:
Fn::Join:
- ''
- - "{"
- ' {"statusCode":200} '
- "}"
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Origin: true
将contentHandling: CONVERT_TO_TEXT
参数添加到两个:集成请求和集成响应设置中的要求较少。
在 swagger CORS 配置中看起来像这样:
responses: {
200: {
description: "Returning CORS headers",
headers: {
"Access-Control-Allow-Headers":{ type: "string" },
"Access-Control-Allow-Methods": { type: "string" },
"Access-Control-Allow-Origin": { type: "string" },
}
}
},
"x-amazon-apigateway-integration": {
type: "mock",
contentHandling: "CONVERT_TO_TEXT", // Resolves problems with cloudfront binary content issues
requestTemplates: {
"application/json": "{ \"statusCode\": 200 }"
},
responses: {
"default": {
statusCode: "200",
contentHandling: "CONVERT_TO_TEXT", // Resolves problems with cloudfront binary content issues
responseParameters: {
"method.response.header.Access-Control-Allow-Headers": "'*'",
"method.response.header.Access-Control-Allow-Methods" : "'*'",
"method.response.header.Access-Control-Allow-Origin" : "'*'"
},
responseTemplates: {
"application/json": "{}"
}
}
}
}
对我来说,问题来自这一行:
"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip", "*/*" ]
只需删除"*/*"
"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip" ]
发现这个之后才看到@bgw的评论
无论如何,希望这可以帮助其他不搜索时间的人!
如果在您的 API 网关设置中,您已将“application/json”添加到二进制媒体类型(例如,如果您想要 gzip 响应),或者如果此请求是针对二进制媒体类型的,则由 ' 添加的默认 OPTIONS需要调整启用 CORS 的功能。
就像上面所说的那样,至少对于集成,您需要将 contentHandling 设置为“CONVERT_TO_TEXT”。 对我来说,它无需将其添加到集成响应中即可工作。 您可以通过 AWS CLI 执行以下操作:
aws apigateway update-integration --rest-api-id YOUR_REST_ID --resource-id YOUR_RESOURCE_ID --http-method OPTIONS --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_TEXT'
哦,不要忘记事后重新部署 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.