Как добавить код состояния http к ответу в flask_restful
С некоторых пор пишу API на flask_restful, как в ответе указать код состояния 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
Ответы (1 шт):
Автор решения: svmitin
→ Ссылка
Нужно удалить jsonify() и через запятую указать желаемый код. С jsonify() так не работает, почему не знаю
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 { 'result': True, 'profile': profile }, 200
else:
# Не верный токен, пользователь не найден
return { 'result': False, 'error': 'User not exists' }, 404