一段时间以来,我一直在写一个关于flask_restful的API,如何在响应中指定http状态码?
欢迎对代码进行提示和评论。
from flask import jsonify, request
from flask_restful import Resource
from dbase import profiles
class Profile(Resource):
"""Операции с профилем"""
def get(self):
"""Получить сведения о своем профиле"""
json_data = request.get_json(force=True)
profile_token = json_data['profile_token']
profile = profiles.get_full_profile(profile_token=profile_token)
if profile:
# Операция прошла успешно
return jsonify({'result': True, 'profile': profile}) # здесь нужен код 200
else:
# Не верный токен, пользователь не найден
return jsonify({'result': False, 'error': 'User not exists'}) # здесь нужен код 404
您需要删除 jsonify() 并指定用逗号分隔的所需代码。jsonify() 不能那样工作,我不知道为什么