flask-restful,如何在响应 类 (add_resource) 中访问 cookie
flask-restful, how to access cookies within Response classes (add_resource)
我的程序是建立在 flask-restful 上的,即对于每个 url 我们都有一个 class 像这样:
class TestAPI(Resource):
def __init__(self, **kwargs):
self.__main_loop = kwargs['main_loop']
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('testKey', type=str, required=True, help='No textKey provided', location='json')
super(TestAPI, self).__init__()
def get(self):
try:
logger.info("Test get")
return {"testAnswer": "Test Value"}
except Exception as e:
logger.error(e)
return e
def post(self):
try:
logger.info("Test post")
args = self.reqparse.parse_args()
logger.info("Items: " + str(args.items()))
return args
except Exception as e:
logger.error(e)
return e
这样添加的:
self.__webserver_rest_api.add_resource(TestAPI, '/api/v1.0/test/', resource_class_kwargs=kwargs)
一切正常,我得到了预期的参数,但是我无法弄清楚如何访问 get 或post 请求。我什至不知道如何访问headers。我在文档中找不到任何内容,也没有找到任何示例。所有示例都使用“正常API”,其中我们有一个适当的请求object.
为清楚起见:我确实知道如何设置和创建 cookie 以及 headers 在构建响应时,这不是问题。
您可以使用 request
object 获取 headers 和 cookies
from flask import request
headers = request.headers
cookies = request.cookies
他们两个return一个字典。
我的程序是建立在 flask-restful 上的,即对于每个 url 我们都有一个 class 像这样:
class TestAPI(Resource):
def __init__(self, **kwargs):
self.__main_loop = kwargs['main_loop']
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('testKey', type=str, required=True, help='No textKey provided', location='json')
super(TestAPI, self).__init__()
def get(self):
try:
logger.info("Test get")
return {"testAnswer": "Test Value"}
except Exception as e:
logger.error(e)
return e
def post(self):
try:
logger.info("Test post")
args = self.reqparse.parse_args()
logger.info("Items: " + str(args.items()))
return args
except Exception as e:
logger.error(e)
return e
这样添加的:
self.__webserver_rest_api.add_resource(TestAPI, '/api/v1.0/test/', resource_class_kwargs=kwargs)
一切正常,我得到了预期的参数,但是我无法弄清楚如何访问 get 或post 请求。我什至不知道如何访问headers。我在文档中找不到任何内容,也没有找到任何示例。所有示例都使用“正常API”,其中我们有一个适当的请求object.
为清楚起见:我确实知道如何设置和创建 cookie 以及 headers 在构建响应时,这不是问题。
您可以使用 request
object 获取 headers 和 cookies
from flask import request
headers = request.headers
cookies = request.cookies
他们两个return一个字典。