使用OPTIONS轮询时出现KrakenD HTTP 405错误



每次通过KrakenD API网关向REST服务发出OPTIONS请求时,我都会收到一个HTTP 405 Method Not Allowed错误。每当我尝试请求REST服务的/apexadapter/version端点时,就会发生这种情况。当我直接从API服务器请求相同的资源时(即,当我绕过KrakenD API网关时(,我不会得到相同的错误我不明白我为什么会收到这个,我希望你能帮助我理解Kraken在做什么,这样我的请求就会出错

根据我读到的一篇题为"启用跨源资源共享(CORS("的文章,我将security/cors添加到全局extra_config中。

下面是curl命令来模拟我的请求:

curl --location --request OPTIONS 'http://localhost:30000/apexadapter/version'

我的krakend.json文件包含以下内容:

{
"version": 2,
"timeout": "15m",
"cache_ttl": "300s",
"output_encoding": "json",
"name": "apexadapter",
"extra_config": {
"security/cors": {
"allow_origins": ["*"],
"allow_methods": ["GET", "HEAD", "POST", "OPTIONS", "PATCH", "DELETE"],
"debug": true
}
},
"endpoints": [
{
"endpoint": "/apexadapter/version",
"method": "GET",
"output_encoding": "no-op",
"backend": [{
"url_pattern": "/version",
"method": "GET",
"encoding": "no-op",
"host": [
"http://apexadapter.apex.svc.cluster.local:38295"
]
}]
},
... and so on

这是Postman(我选择的测试API的工具(输出的结果

OPTIONS http://localhost:30000/apexadapter/version: {
"Network": {
"addresses": {
"local": {
"address": "::1",
"family": "IPv6",
"port": 51269
},
"remote": {
"address": "::1",
"family": "IPv6",
"port": 30000
}
}
},
"Request Headers": {
"user-agent": "PostmanRuntime/7.29.2",
"accept": "*/*",
"cache-control": "no-cache",
"postman-token": "bf91c1ff-85fe-41e1-ad17-33afee354b2c",
"host": "localhost:30000",
"accept-encoding": "gzip, deflate, br",
"connection": "keep-alive"
},
"Response Headers": {
"content-type": "text/plain",
"date": "Fri, 12 Aug 2022 19:40:38 GMT",
"content-length": "22"
},
"Response Body": "405 method not allowed"
}

我注意到响应中的响应标头被完全剥离了。尽管没有操作,但我的Access-Control-Allow-*标头都没有返回。

编辑:我向维护人员提出了一个问题:https://github.com/krakendio/krakend-ce/issues/545

这听起来不像是CORS特定的问题,因为这些问题在客户端中显示为错误,指示无效或不存在CORS响应标头。

考虑到HTTP 405 Method Not Allowed错误消息,您的问题几乎可以肯定是您的web服务器根本没有设置为允许OPTIONS请求:大多数web服务器(例如Apache(默认情况下只允许GET和POST请求方法,并且必须更改其配置以允许OPTIONS请求。

一旦您允许OPTIONS请求,那么根据您的CORS配置是否正确,您的应用程序将工作,或者将向客户端发送CORS特定的错误消息,您应该能够弄清楚。

在额外的配置中只需添加auto_options:true。

例如:

{
"extra_config": {
.
.
"router": {
"auto_options": true
}
}

这将附加到您的每个请求选项方法。你所拥有的与紧身胸衣问题无关。:(

最新更新