2015-11-06 96 views
0

我想使用POST謂詞在flask-restplus上對虛擬機執行操作,但當沒有主體時總是會產生400。Flask-restplus如何在沒有身體的情況下發帖

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 

結果是 { 「消息」:「瀏覽器(或代理)發送的請求,該服務器無法理解」。 }

如果我只是簡單地改變從發佈到獲得,它工作正常。但是,我真的很想使用POST動詞,因爲那是我需要遵循的自定義非CRUD操作的標準動詞。我是否用flask-restplus將自己繪製成一個角落?

注意:對於需要身體的操作,它可以正常工作。它唯一的無身體瓶 - restplus後操作,400空體錯誤。

回答

0

如果你設置的內容類型爲application/json我認爲你的身體應至少爲{}。 如果你想提交一個空的有效載荷,只需刪除內容類型的頭。

我想這也正是這個問題(這我想弄清楚):https://github.com/noirbizarre/flask-restplus/issues/84

+0

是的,這就是它。我不需要將body設置爲{},但是如果我刪除了「header-Content」類型的應用程序/ json,它就起作用了!我看到你將它添加爲0.8.1里程碑。太棒了,謝謝。 –

0

這裏是爲我工作把我抱過,直到另一種解決方案是找到了一個解決辦法:

@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 
相關問題