TypeError: 'dict' object is not callable when using request.json()

TypeError: 'dict' object is not callable when using request.json()

每次我尝试通过 request.json() 从请求中获取 JSON 时,都会发生错误 'dict object is not callable'。例如,

from bottle import run, request, post

@post('/')
def test():
    data = request.json()

run(host='localhost',port=8080)

用curl测试一下,

 curl -H "Content-Type: application/json" -d '{"test":"test"}' -X POST http://localhost:8080/

这是输出,

Traceback (most recent call last):
  File "C:\Users\javir\AppData\Local\Programs\Python\Python38\lib\site-packages\bottle.py", line 868, in _handle 
    return route.call(**args)
  File "C:\Users\javir\AppData\Local\Programs\Python\Python38\lib\site-packages\bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "d:/Workspace Universidad/SD-P3/test.py", line 5, in test
    data = request.json()
TypeError: 'dict' object is not callable
127.0.0.1 - - [12/May/2020 12:43:42] "POST / HTTP/1.1" 500 741

无论内容如何,​​我总是遇到同样的错误。有谁知道它会是什么?

在 python 中,当您在变量后包含 () 时,会出现 "call"。 TypeError: 'dict' object is not callable 只是意味着 request.json 是一个 dict 并且不能被调用。去掉request.json后的()

@post('/')
def test():
    data = request.json

应该是data = request.json而不是data = request.json()