gpt4 book ai didi

Azure API 管理返回 "404 - resource not found"但端点测试有效

转载 作者:行者123 更新时间:2023-12-02 23:16:38 25 4
gpt4 key购买 nike

有谁知道为什么对 APIM 的 PUT 请求返回 404“资源未找到”,但其他操作类型返回 HTTP 200?

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

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

postman 输出:

{
"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 - 方法不允许。这让问题变得显而易见。

关于Azure API 管理返回 "404 - resource not found"但端点测试有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63530506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com