python-eve: PATCH 项/文档出现 405 错误

python-eve: 405 error on PATCH item / document

我正在使用 eve-API 访问 MongoDB。但是,如果我想 PATCH 一个项目(文档),我会收到 405 错误。

我可以使用 curl 访问我想要更新的文档,如下所示:

curl 127.0.0.1:5000/simulations/20151223_011329620

APIreturns所需文件:

{
    "_updated": "Wed, 23 Dec 2015 00:13:29 GMT",
    "regenerator": "565e12c58b724d7884cd02bb",
    "identifier": "20151223_011329620",
    "_etag": "9c50633f1bf34bcefb84237ce2477066529f3c0e",
    "_links": {
        "parent": {
            "title": "home",
            "href": "/"
        },
        "self": {
            "title": "Simulation",
            "href": "simulations/5679e72904c8880421b0abfa"
        },
        "collection": {
            "title": "simulations",
            "href": "simulations"
        }
    },
    "_created": "Wed, 23 Dec 2015 00:13:29 GMT",
    "status": "pending",
    "_id": "5679e72904c8880421b0abfa",
    "sectors": ["565e12c58b724d7884cd02b9", "565e12c58b724d7884cd02ba"]
}

我尝试使用 curl 来通过这样的 PATCH 请求更新文档:

curl -X PATCH -d '{"status": "pending"}' http://127.0.0.1:5000/simulations/20151223_011329620

因为这给了我一个 405 错误,所以我阅读了 docs 告诉我应该提供一个有效的 etag。因此,我尝试了:

curl -H "If-Match: 9c50633f1bf34bcefb84237ce2477066529f3c0e" -H "Content-Type: application/json" -X PATCH http://127.0.0.1:5000/simulations/20151223_011329620 -d '{"status": "pending"}'

这给了我同样的 405 错误:

{"_status": "ERR", "_error": {"code": 405, "message": "The method is not allowed for the requested URL."}}

我不知道为什么我不能PATCH想要的项目(文档),因为全局配置如下:

RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']

根据 docs,我可以用等效的小写字母覆盖这些全局设置(resource_methodsitem_methods) 在端点的定义中。因此,这些似乎都没有默认值,我也没有在端点的定义中指定它们(下面的 config 片段)我找不到这个错误的原因。

config = {
    'additional_lookup': {
        'url': r'regex("\d{8}_\d{9}(_\d{3})?")',
        'field': 'identifier'
    },
    'schema': schema,
}

PATCH默认只能使用_id字段。如果您想使用其他字段进行 PATCH,那么您需要在该配置中声明一个 ID_FIELD 命名字段,该字段将指向配置中的其他字段,例如您的 "identifier"。

编辑:

ID_FIELD : Name of the field used to uniquely identify resource items within the database. You want this field to be properly indexed on the database. Can be overridden by resource settings. Defaults to _id.

有关更多信息,请阅读此处如何在 settings.py 文件中设置 ID_FIELD:http://python-eve.org/config.html