我使用Flask-RESTful来实现一个简单的API,我想支持HTTP TRACE方法。我有GET和PUT方法,但我无法弄清楚如何从框架中获得的“请求”结构中获取整个请求,包括标头和数据。
我尝试使用请求结构创建响应;
import threading
from flask import Flask, Response
from flask_restful import Api, Resource, request
class RestfulClient(threading.Thread):
def __init__(self, config):
threading.Thread.__init__(self)
self._app = Flask(__name__)
self._api = Api(self._app)
self._api.add_resource(TraceAPI, '/', endpoint="trace")
def run(self):
self._app.run()
class TraceAPI(Resource):
def trace(self):
return Response(request, status=200, mimetype='message/http')
但这会导致错误;
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
我可以通过从响应中删除请求来做一个简单的响应;
def trace(self):
return Response(status=200, mimetype='message/http')
或仅返回请求的数据部分;
def trace(self):
return Response(request.data, status=200, mimetype='message/http')
但为了符合RFC7230,我想我需要返回整个HTTP请求。这可能在FlaskRESTful框架内吗?