简体   繁体   English

Flask-restplus如何在没有身体的情况下张贴

[英]Flask-restplus how to post without a body

I'd like to use the POST verb to perform actions on a VM with flask-restplus, but it always results in a 400 when there is no body. 我想使用POST动词在带有flask-restplus的VM上执行操作,但是在没有主体的情况下,它总是导致400。

VM_ACTION_FIELDS = {
      'vmActionId': fields.Integer(required=True, description='The vmActionId of the VmAction'),
      'vmId': fields.Integer(required=True, description='The vmId of the VmAction'),
      'status': fields.String(required=True, description='The status of the VmAction',
                              enum=['NEW', 'REQUESTED', 'IN_PROGRESS', 'ERROR', 'COMPLETED']),
      'actionType': fields.String(required=True, description='The actionType of the VmAction',
                                  enum=['STOP', 'RESTART']),
      'createdAt': fields.DateTime(required=True,
                                   description='The createdAt datetime of the VmAction'),
      'completedAt': fields.DateTime(required=True,
                                     description='The completedAt datetime of the VmAction'),
  }
  VM_ACTION_MODEL = api.model('VmAction', VM_ACTION_FIELDS)

  [snip]

      @vms_ns.route('/<int:vmId>/stop', endpoint='vmStop')
      class VmStopView(Resource):
          """
          Stop a VM
          """
          @api.marshal_with(VM_ACTION_MODEL, code=202)
          @api.doc(id='stopVm', description='Stop a Vm')
          def post(self, vmId):
              # do stuff 
              return vmAction, 202

The result is 400 { "message": "The browser (or proxy) sent a request that this server could not understand." 结果为400 {“ message”:“浏览器(或代理)发送了该服务器无法理解的请求。” } }

If I simply change from post to get, it works fine. 如果我只是换个职位,那就可以了。 BUT, I really want to use the POST verb for this, because thats the standard verb I need to follow for custom non-CRUD actions. 但是,我真的想为此使用POST动词,因为多数民众赞成在我用于自定义非CRUD操作时要遵循的标准动词。 Have I painted myself into a corner with flask-restplus? 我是否已将烧瓶剩余部分涂在角落里?

Note: for operations that require a body, it works fine. 注意:对于需要身体的操作,它可以正常工作。 Its only bodyless flask-restplus post operations that 400 error on empty body. 它唯一的无瓶长颈瓶restplus后期操作会在空体上显示400错误。

If you're setting the content-type to application/json I think you're body should be at least {} . 如果您将内容类型设置为application/json我认为您的身体至少应为{} If you want to submit an empty payload, just remove the content-type header. 如果要提交空的有效负载,只需删除content-type标头即可。

I think this is exactly this issue (which I'm trying to figure out): https://github.com/noirbizarre/flask-restplus/issues/84 我认为这正是这个问题(我正在尝试解决): https : //github.com/noirbizarre/flask-restplus/issues/84

Here is a workaround that is working for me to hold me over until another solution is found: 这是一种解决方法,对我来说一直有效,直到找到其他解决方案为止:

@app.before_request
  def before_request():
      """This is a workaround to the bug described at
      https://github.com/noirbizarre/flask-restplus/issues/84"""
      ctlen = int(request.headers.environ.get('CONTENT_LENGTH', 0))
      if ctlen == 0:
          request.headers.environ['CONTENT_TYPE'] = None

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM