简体   繁体   English

flask.ext.restful-> reqparse

[英]flask.ext.restful -> reqparse

I'm trying to use this reqparse to send meaningful validation back to my clients in a flask app. 我正在尝试使用此reqparse将有意义的验证发送回Flask应用程序中的客户端。 Here is the code: 这是代码:

class ParserTest(Resource):

    def get(self):
        parser = reqparse.RequestParser();
        parser.add_argument('not_sent_from_client', type=str, required=True,
                help='This field is required',
                location='form')
        args = parser.parse_args()
        return "ok"

restfulApi.add_resource(ParserTest, '/p')

When I curl -v http://localhost:5000/p I get this response: 当我curl -v http://localhost:5000/p我得到以下响应:

* About to connect() to localhost port 5000 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 5000 (#0) GET /api/v1/p HTTP/1.1 User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 Host: localhost:5000 Accept:
*/*

* HTTP 1.0, assume close after body < HTTP/1.0 400 BAD REQUEST < Content-Type: text/html < Content-Length: 192 < Server: Werkzeug/0.8.3 Python/2.7.1 < Date: Sat, 22 Dec 2012 22:21:33 GMT <  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>400 Bad Request</title> <h1>Bad Request</h1> <p>The browser (or proxy) sent a request that this server could not understand.</p>
* Closing connection #0

Where did the error message go? 错误消息到哪里去了?

When I change the code to this: 当我将代码更改为此:

def get(self):
        try:
            parser = reqparse.RequestParser();
            parser.add_argument('not_sent_from_client', type=str, required=True,
                help='This field is required',
                location='form')
            args = parser.parse_args()
        except Exception as e:
            pprint.pprint(getmembers(e))
        return 'ok'

This gets dumped to stdout when I make a GET request... 当我发出GET请求时,这将转储到stdout ...

[('__call__',   <bound method ClientDisconnected.__call__ of <ClientDisconnected '400: Bad Request'>>),  
('__class__', <class 'werkzeug.exceptions.ClientDisconnected'>),  
('__delattr__',   <method-wrapper '__delattr__' of ClientDisconnected object at 0x1065ca230>),  ('__dict__', {'data': {'message': 'This field is required'}}),  
('__doc__',   'Internal exception that is raised if Werkzeug detects a disconnected\n    client.  Since the client is already gone at that point attempting to\n    send the error message to the client might not work and might ultimately\n    result in another exception in the server.  Mainly this is here so that\n    it is silenced by default as far as Werkzeug is concerned.\n\n    Since disconnections cannot be reliably detected and are unspecified\n    by WSGI to a large extend this might or might not be raised if a client\n is gone.\n\n    .. versionadded:: 0.8\n    '),  ('__format__',   <built-in method __format__ of ClientDisconnected object at 0x1065ca230>),  ('__getattribute__',   <method-wrapper '__getattribute__' of ClientDisconnected object at 0x1065ca230>),  ('__getitem__',   <method-wrapper '__getitem__' of ClientDisconnected object at 0x1065ca230>),  ('__getslice__',   <method-wrapper '__getslice__' of ClientDisconnected object at 0x1065ca230>),  ('__hash__',   <method-wrapper '__hash__' of ClientDisconnected object at 0x1065ca230>),  ('__init__',   <bound method ClientDisconnected.__init__ of <ClientDisconnected '400: Bad Request'>>),  ('__module__', 'werkzeug.exceptions'),  ('__new__', <built-in method __new__ of type object at 0x105bd4570>),  ('__reduce__',   <built-in method __reduce__ of ClientDisconnected object at 0x1065ca230>),  ('__reduce_ex__',   <built-in method
__reduce_ex__ of ClientDisconnected object at 0x1065ca230>),  ('__repr__',   <bound method ClientDisconnected.__repr__ of <ClientDisconnected '400: Bad Request'>>),  ('__setattr__',   <method-wrapper '__setattr__' of ClientDisconnected object at 0x1065ca230>),  ('__setstate__',   <built-in method __setstate__ of ClientDisconnected object at 0x1065ca230>),  ('__sizeof__',   <built-in method __sizeof__ of ClientDisconnected object at 0x1065ca230>),  ('__str__',   <bound method ClientDisconnected.__str__ of <ClientDisconnected '400: Bad Request'>>),  ('__subclasshook__',   <built-in method __subclasshook__ of type object at 0x105da42c0>),  ('__unicode__',   <bound method ClientDisconnected.__unicode__ of <ClientDisconnected '400: Bad Request'>>),  ('__weakref__', None),  ('args', ('400 Bad Request',)),  ('code', 400),  ('data', {'message': 'This field is required'}),  ('description',   '<p>The browser (or proxy) sent a request that this server could not understand.</p>'),  ('get_body',   <bound method ClientDisconnected.get_body of <ClientDisconnected '400: Bad Request'>>),  ('get_description',   <bound method ClientDisconnected.get_description of <ClientDisconnected '400: Bad Request'>>),  ('get_headers',   <bound method ClientDisconnected.get_headers of <ClientDisconnected '400: Bad Request'>>),  ('get_response',   <bound method ClientDisconnected.get_response of <ClientDisconnected '400: Bad Request'>>),  ('message', '400 Bad Request'),  ('name', 'Bad Request'),  ('wrap',   <bound method type.wrap of <class 'werkzeug.exceptions.ClientDisconnected'>>)]

But "ok" gets sent to the client. 但是“确定”会发送给客户端。 So the client is disconnecting. 因此客户端正在断开连接。

I want to use this to send back meaningful error messages to my apis clients but it seems like another error is happening in the call stack. 我想使用它向我的api客户端发送回有意义的错误消息,但似乎在调用堆栈中发生了另一个错误。 Any ideas? 有任何想法吗?

it seems that Werkzeug changed their exceptions logic some months ago with https://github.com/mitsuhiko/werkzeug/commit/2723a117e34207f33ff075a9ea6d1f28a33131d3 . 似乎几个月前Werkzeug使用https://github.com/mitsuhiko/werkzeug/commit/2723a117e34207f33ff075a9ea6d1f28a33131d3更改了异常逻辑。

Hence flask-restful is a bit outdated. 因此,烧瓶不稳定有点过时了。 I had the same problem and I did a quick hack in order to adapt to the new code, have a look at the changes in https://github.com/vojnovski/flask-restful/commit/8bfd1a8427b15df95a6549afdf4ddb4a49d8f29b . 我遇到了同样的问题,为了适应新代码,我进行了快速修改,看看https://github.com/vojnovski/flask-restful/commit/8bfd1a8427b15df95a6549afdf4ddb4a49d8f29b中的更改。

Have in mind that his also breaks the tests of flask-restful and you need to patch them too if you want to have no test failures. 请记住,他也破坏了烧瓶测试的测试,如果您不希望测试失败,也需要对其进行修补。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有名为 flask.ext.restful 的模块 - No module named flask.ext.restful 如何为不同的请求添加不同的flask-restful reqparse要求 - How to add different flask-restful reqparse requirements for different requests 来自 reqparse flask_Restful 的 parse_args 返回错误 - parse_args from reqparse flask_Restful returning error Flask 二进制的 Restful reqparse add_argument 类型 - Flask Restful reqparse add_argument type for binary 为什么我无法使用add_argument()将HTTP标头发送到Flask-RESTful reqparse模块? - Why am I unable to send HTTP Headers to Flask-RESTful reqparse module using add_argument()? 使用flask_restful 的reqparse,您是否可以忽略params 或json 中未包含的值? - Using flask_restful's reqparse, are you able to ignore values not included in params or json? flask_restful没有使用于flask.ext.login的login_required装饰器 - flask_restful not working with flask.ext.login's login_required decorator 自定义错误消息,要求重新获得flask-restplus(V 0.12.1) - Custom error messages with reqparse of flask-restplus (V 0.12.1) flask reqparse 如何使用 action='append' 获取空列表(默认情况下不是) - flask reqparse how to get empty list using action='append' (not by default) 为什么列表只返回我的 Flask API reqParse arguments 中的第一个元素? - Why is the list returning just the first element in my Flask API reqParse arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM