【问题标题】:Azure API Management returns "404 - resource not found" but endpoint testing worksAzure API 管理返回“404 - 找不到资源”,但端点测试有效
【发布时间】:2020-08-21 21:53:54
【问题描述】:

有没有人知道为什么对 APIM 的 PUT 请求返回 404“未找到资源”而其他操作类型返回 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 端点配置吗?
  • 我添加了我认为可行的内容。如果我需要添加更多细节,请告诉我。
  • 另外,请您指定您对 APIM 的调用。
  • 我已经清除了配置中的所有调用,但只有一个调用。我们使用/api/v{version}/Tasks/{taskId}/Complete,特别是/api/v1/Tasks/3/Complete

标签: azure azure-api-management


【解决方案1】:

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>

设置&lt;method&gt;PUT&lt;/method&gt; 已解决此问题。

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

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 2019-03-17
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2019-10-25
    • 2015-04-27
    相关资源
    最近更新 更多