如何提取Flask返回的JSON数据?
后端:
response_obj = {
'status': 'fail',
'message': 'Invalid Payload'
}
return jsonify(response_obj), 500
前端:
import requests
data = # some json data here
response = requests.post('http://18.133.238.8/auth/login', json=data)
我尝试过:
print(dir(response))
这给了我
...
...
__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
提示我使用的:
print(response.json)
或:
print(response.json())
我只是遇到错误。如何从响应中获取JSON数据?
答案 0 :(得分:0)
尝试这种方式:
from requests import request
response = request('POST', "<URL here>")
print(response.json())