代码之家  ›  专栏  ›  技术社区  ›  Thegreatcatt

Python:如何解决“TypeError:'str'对象不可调用”

  •  0
  • Thegreatcatt  · 技术社区  · 2 年前

    所以我有这个flask api脚本

    @app.route('/request', methods=["POST"])
    def reqpost():
         content = request.get_json()
         h = content['type']
         if h == 'one'():
              typeone()
              return ("running type one")
         elif h == 'two'():
              typetwo()
              return ("running type two")
         else:
              return ("error!")
    

    这是在请求中发送的json数据

    {
      "type": "typeone"
    }
    

    但是当我向服务器发送请求时,我会在服务器输出中得到这个

    TypeError:“str”对象不可调用

    对于这些线路

    if h == 'one'():
      # stuff
    # and
    elif h == 'two'():
      # stuff
    

    我曾尝试在这些行中添加str(),但仍然会出现那个愚蠢的错误

    任何帮助都很好,谢谢:)

    1 回复  |  直到 2 年前
        1
  •  0
  •   Tal Folkman    2 年前

    去蒂 () :

    @app.route('/request', methods=["POST"])
    def reqpost():
         content = request.get_json()
         h = content['type']
         if h == 'one':
              typeone()
              return ("running type one")
         elif h == 'two':
              typetwo()
              return ("running type two")
         else:
              return ("error!")