Azure API 管理 returns“404 - 未找到资源”但端点测试有效
Azure API Management returns "404 - resource not found" but endpoint testing works
有人知道为什么 PUT 请求 APIM return 404“未找到资源”但其他操作类型 return HTTP 200 吗?
我可以使用 APIM 中的测试功能来调用 PUT 操作端点,我可以检查后端 Web 应用程序上的控制台输出并查看调用是否成功。但是,当使用 Postman 或前端 Web 应用程序时,我们会收到找不到资源的错误消息。
我真的很困惑,因为如前所述,其他动词也可以。我们从 Swagger 生成 API 端点定义,因此它与用于定义其他端点的方法完全相同。
邮递员输出:
{
"statusCode": 404,
"message": "Resource not found"
}
编辑:端点配置
{
"openapi": "3.0.1",
"info": {
"title": "Foo",
"description": "",
"version": "1.0"
},
"servers": [{
"url": "https://custom.domain.com"
}],
"paths": {
"/api/v{version}/Tasks/{taskId}/Complete": {
"put": {
"tags": ["Tasks"],
"summary": "/api/v{version}/Tasks/{taskId}/Complete - PUT",
"operationId": "put-api-v-version-tasks-taskid-complete",
"parameters": [{
"name": "taskId",
"in": "path",
"description": "Format - int64.",
"required": true,
"schema": {
"type": "integer"
}
}, {
"name": "version",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
}
}
},
"responses": {
"200": {
"description": "Success"
},
"400": {
"description": "Bad Request",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
}
}
}
}
}
}
}
}
APIM 中的策略未设置为允许 PUT 方法。
<policies>
<inbound>
<cors allow-credentials="true">
<allowed-origins>
<origin>https://URL</origin>
</allowed-origins>
<allowed-methods>
<method>GET</method>
<method>POST</method>
<method>OPTIONS</method>
<method>PUT</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
<expose-headers>
<header>*</header>
</expose-headers>
</cors>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
设置 <method>PUT</method>
已解决此问题。
Postman 查询发送的是 POST 请求而不是 PUT 请求。当我们将 Postman 更改为正确的方法时,我们得到了 405 - 方法不允许。这使问题变得显而易见。
有人知道为什么 PUT 请求 APIM return 404“未找到资源”但其他操作类型 return HTTP 200 吗?
我可以使用 APIM 中的测试功能来调用 PUT 操作端点,我可以检查后端 Web 应用程序上的控制台输出并查看调用是否成功。但是,当使用 Postman 或前端 Web 应用程序时,我们会收到找不到资源的错误消息。
我真的很困惑,因为如前所述,其他动词也可以。我们从 Swagger 生成 API 端点定义,因此它与用于定义其他端点的方法完全相同。
邮递员输出:
{
"statusCode": 404,
"message": "Resource not found"
}
编辑:端点配置
{
"openapi": "3.0.1",
"info": {
"title": "Foo",
"description": "",
"version": "1.0"
},
"servers": [{
"url": "https://custom.domain.com"
}],
"paths": {
"/api/v{version}/Tasks/{taskId}/Complete": {
"put": {
"tags": ["Tasks"],
"summary": "/api/v{version}/Tasks/{taskId}/Complete - PUT",
"operationId": "put-api-v-version-tasks-taskid-complete",
"parameters": [{
"name": "taskId",
"in": "path",
"description": "Format - int64.",
"required": true,
"schema": {
"type": "integer"
}
}, {
"name": "version",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"
}
}
}
},
"responses": {
"200": {
"description": "Success"
},
"400": {
"description": "Bad Request",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"
}
}
}
}
}
}
}
}
}
APIM 中的策略未设置为允许 PUT 方法。
<policies>
<inbound>
<cors allow-credentials="true">
<allowed-origins>
<origin>https://URL</origin>
</allowed-origins>
<allowed-methods>
<method>GET</method>
<method>POST</method>
<method>OPTIONS</method>
<method>PUT</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
<expose-headers>
<header>*</header>
</expose-headers>
</cors>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
设置 <method>PUT</method>
已解决此问题。
Postman 查询发送的是 POST 请求而不是 PUT 请求。当我们将 Postman 更改为正确的方法时,我们得到了 405 - 方法不允许。这使问题变得显而易见。