[英]AWS API gateway error while making OPTIONS request
I am using CFT for creating env for my API. I have added OPTIONS for CORS. I have noticed when i do the test from AWS console for OPTIONS
i am getting 200
response.我正在使用 CFT 为我的 API 创建环境。我已经为 CORS 添加了选项。我注意到当我从
AWS console for OPTIONS
进行测试时,我得到了200
响应。 However when i do the same from CURL or PostMan
i am getting 500
internal server error.但是,当我从
CURL or PostMan
执行相同操作时,我收到500
内部服务器错误。 After reviewing SO questions related to it.在审查了与之相关的SO问题之后。 I have modified the Integration reponse to CONVERT_TO_TEXT.
我已将集成响应修改为 CONVERT_TO_TEXT。 but that also did not solve the issue.
但这也没有解决问题。
I have noticed a wired behavior in the log.我注意到日志中有一个有线行为。 Following is the log snippet for request from AWS console:
以下是来自 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
But the same request when i give from CRUL or PM i am seeing the following log:但是当我从 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
We can see it is trying to transform the [Binary Data]
but i am not sending anything.我们可以看到它正在尝试转换
[Binary Data]
但我没有发送任何东西。
Curl i used: curl -X OPTIONS -H "Access-Control-Request-Headers: Content-Type" -H "Access-Control-Request-Method: POST" -H "Access-Control-Allow-Origin: '*'" -v MYHOST
Curl 我用过:
curl -X OPTIONS -H "Access-Control-Request-Headers: Content-Type" -H "Access-Control-Request-Method: POST" -H "Access-Control-Allow-Origin: '*'" -v MYHOST
Why i am seeing this difference in log?为什么我在日志中看到这种差异? what went wrong in my configurtion?
我的配置出了什么问题? Can you help me.
你能帮助我吗。
UPDATE: I am using the below CFT更新:我正在使用下面的 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
there seams to be a less documented requirement to add contentHandling: CONVERT_TO_TEXT
param to both: integration request and integration response settings.将
contentHandling: CONVERT_TO_TEXT
参数添加到两个:集成请求和集成响应设置中的要求较少。
In swagger CORS config would look something like that:在 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": "{}"
}
}
}
}
For me the issue was coming from this line:对我来说,问题来自这一行:
"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip", "*/*" ]
Just remove the "*/*"
只需删除
"*/*"
"x-amazon-apigateway-binary-media-types" : [ "multipart/form-data", "application/zip" ]
It's only after finding this that I saw the comment from @bgw发现这个之后才看到@bgw的评论
Anyway, hope this can help someone else not searching for hours!无论如何,希望这可以帮助其他不搜索时间的人!
If in your API gateway settings you have added 'application/json' to the binary media types (like in case you want to gzip responses) or if this request is anyway for a binary media type then the default OPTIONS that gets added by the 'enable CORS' feature will need to be adjusted.如果在您的 API 网关设置中,您已将“application/json”添加到二进制媒体类型(例如,如果您想要 gzip 响应),或者如果此请求是针对二进制媒体类型的,则由 ' 添加的默认 OPTIONS需要调整启用 CORS 的功能。
Like it was said above, you'll need to set the contentHandling to "CONVERT_TO_TEXT" at least for the integration.就像上面所说的那样,至少对于集成,您需要将 contentHandling 设置为“CONVERT_TO_TEXT”。 For me it worked without having to add it to the integration response.
对我来说,它无需将其添加到集成响应中即可工作。 You can do it via AWS CLI as follows:
您可以通过 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'
Oh and don't forget to redeploy the API afterwards.哦,不要忘记事后重新部署 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.