2

我想在 POST 和其他修改方法中使用查询参数。这可能适用于常规查询参数或通过带有变量的 url 规则提取的参数。但我注意到 Eve 专门为 POST 调用删除了这些参数。我可以轻松地进行必要的更改以保留它们,但由于它们是故意丢弃的,我想知道让它们存在是否有一些缺点。这个设计决定的原因是什么?

示例用例

也许有一个问题,为什么有人想要使用这样的查询参数。我想到的 API 在 POST 和 DELETE 调用中使用查询参数。为什么这可能很方便的一个例子是允许用户修改调用的验证和实际行为。举一个有点做作的例子:

DELETE /resource/123
    -- fails if there are dependent objects for this resource
DELETE /resource/123?cascade=true  
    -- allow delete to cascade to dependent objects (eg. user clicked "I am sure")

另一个例子:

POST /user?allowId=True  { "id" : 123, "name" : "Bob }
    -- will accept externally-defined ID as opposed to generating a new one

另一个例子:

POST /container/<foo>/resource    { ... }
    -- create a new resource inside of the <foo> container

已编辑

我尝试使用 request.args 来获取变量规则,但它似乎不起作用。(可以通过这种方式获取查询参数,但我希望变量规则也能正常工作。)

*in settings.py*
DOMAIN = {
    'ducks' : {
        'url':'rows/<row>/ducks',
        'schema': {
            'name' : { 'type' : 'string' }
        }
    }
}

*command line*
curl -H "Content-Type: application/json" -X POST 'http://localhost:5001/rows/1/ducks' -d '{"name":"bob"}

当代码进入我的 on_pre_POST 钩子时,request.args 是空的。如果我走上堆栈,我会看到 inendpoints.collection_endpoint()函数lookup确实包含{'row':1}但它没有传递到post(response)调用中。如果有意义的话,我可以提交一个拉取请求来解决这个问题。

已编辑 2

我发现这些参数request.view_argsrequest.args. 这意味着这两种参数都可以在 flask/eve 中的任何地方访问,而无需更改代码。

4

1 回答 1

1

由于on_pre_POST传递了请求对象,您可以轻松实现:

def my_pre_post_callback(resource, request):
    allowId = request.args.get('allowId')
    ...
于 2014-11-07T17:45:45.853 回答