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

问题描述

有人对PUT向APIM的请求为何返回404“找不到资源”而其他操作类型返回HTTP 200有任何想法吗?

我可以使用API​​M中的测试功能来调用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","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-不允许的方法。这使问题变得很明显。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...