簡體   English   中英

Flask-restplus如何在沒有身體的情況下張貼

[英]Flask-restplus how to post without a 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

結果為400 {“ message”:“瀏覽器(或代理)發送了該服務器無法理解的請求。” }

如果我只是換個職位,那就可以了。 但是,我真的想為此使用POST動詞,因為多數民眾贊成在我用於自定義非CRUD操作時要遵循的標准動詞。 我是否已將燒瓶剩余部分塗在角落里?

注意:對於需要身體的操作,它可以正常工作。 它唯一的無瓶長頸瓶restplus后期操作會在空體上顯示400錯誤。

如果您將內容類型設置為application/json我認為您的身體至少應為{} 如果要提交空的有效負載,只需刪除content-type標頭即可。

我認為這正是這個問題(我正在嘗試解決): https : //github.com/noirbizarre/flask-restplus/issues/84

這是一種解決方法,對我來說一直有效,直到找到其他解決方案為止:

@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