Is it possible to implement HTTP TRACE using Flask-RESTful?

707 views Asked by At

I am using Flask-RESTful to implement a simple API, and I want to support the HTTP TRACE method. I have the GET and PUT methods working but I can't figure out how to get the entire request, including headers and data, from the "request" structure I get from the framework.

I have tried creating a response with the request structure;

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')        

But that results in the error;

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.

I can do a simple response by removing the request from the response;

def trace(self):        
    return Response(status=200, mimetype='message/http')        

Or by only returning the data part of the request;

def trace(self):        
    return Response(request.data, status=200, mimetype='message/http')        

But in order to be RFC7230 compliant I think I need to return the whole HTTP request. Is this possible within the FlaskRESTful framework?

0

There are 0 answers