Azure API 管理返回“404 - 未找到资源”,但端点测试有效

Dam*_*per 3 azure azure-api-management

有人知道为什么对 APIM 的 PUT 请求返回 404 \xe2\x80\x9cResource not found\xe2\x80\x9d 但其他操作类型返回 HTTP 200 吗?

\n

我可以使用 APIM 中的测试功能来调用 PUT 操作端点,并且可以检查后端 Web 应用程序上的控制台输出并查看调用是否通过。但是,当使用 Postman 或前端 Web 应用程序时,我们会收到“资源未找到”错误消息。

\n

我真的很困惑,因为正如前面提到的,其他动词也可以正常工作。我们从 Swagger 生成 API 端点定义,因此它与用于定义其他端点的方法完全相同。

\n

邮递员输出:

\n
{\n    "statusCode": 404,\n    "message": "Resource not found"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

编辑:端点配置

\n
{\n    "openapi": "3.0.1",\n    "info": {\n        "title": "Foo",\n        "description": "",\n        "version": "1.0"\n    },\n    "servers": [{\n        "url": "https://custom.domain.com"\n    }],\n    "paths": {\n        "/api/v{version}/Tasks/{taskId}/Complete": {\n            "put": {\n                "tags": ["Tasks"],\n                "summary": "/api/v{version}/Tasks/{taskId}/Complete - PUT",\n                "operationId": "put-api-v-version-tasks-taskid-complete",\n                "parameters": [{\n                    "name": "taskId",\n                    "in": "path",\n                    "description": "Format - int64.",\n                    "required": true,\n                    "schema": {\n                        "type": "integer"\n                    }\n                }, {\n                    "name": "version",\n                    "in": "path",\n                    "required": true,\n                    "schema": {\n                        "type": "string"\n                    }\n                }],\n                "requestBody": {\n                    "content": {\n                        "application/json": {\n                            "schema": {\n                                "$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"\n                            }\n                        },\n                        "text/json": {\n                            "schema": {\n                                "$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"\n                            }\n                        },\n                        "application/*+json": {\n                            "schema": {\n                                "$ref": "#/components/schemas/Foo.Tasks.TaskStatusRequest"\n                            }\n                        }\n                    }\n                },\n                "responses": {\n                    "200": {\n                        "description": "Success"\n                    },\n                    "400": {\n                        "description": "Bad Request",\n                        "content": {\n                            "text/plain": {\n                                "schema": {\n                                    "$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"\n                                }\n                            },\n                            "application/json": {\n                                "schema": {\n                                    "$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"\n                                }\n                            },\n                            "text/json": {\n                                "schema": {\n                                    "$ref": "#/components/schemas/Microsoft.AspNetCore.Mvc.ProblemDetails"\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Dam*_*per 5

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>
Run Code Online (Sandbox Code Playgroud)

设置<method>PUT</method>已经解决了这个问题。

Postman 查询发送的是 POST 请求,而不是 PUT 请求。当我们将 Postman 更改为正确的方法时,我们得到了 405 - 方法不允许。这让问题变得显而易见。