0

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 { "メッセージ": "ブラウザ (またはプロキシ) が、このサーバーが理解できない要求を送信しました。" }

post から get に変更するだけで問題なく動作します。しかし、これには POST 動詞を使用したいと本当に思っています。これは、カスタムの非 CRUD アクションに従う必要がある標準的な動詞だからです。フラスコレストプラスで自分を隅に追いやったことがありますか?

注: 本体を必要とする操作の場合、正常に機能します。空の本体で 400 エラーが発生する唯一の本体のないフラスコ-レストプラス ポスト操作。

4

2 に答える 2

0

content-type をapplication/jsonに設定している場合、body は少なくとも{}. 空のペイロードを送信する場合は、コンテンツ タイプ ヘッダーを削除するだけです。

これはまさにこの問題だと思います(私は理解しようとしています):https://github.com/noirbizarre/flask-restplus/issues/84

于 2015-11-06T06:40:40.313 に答える
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
于 2015-11-07T20:07:27.210 に答える