[Zope3-Users] Accessing raw post data

Jim Washington washington.jim at gmail.com
Wed Aug 25 09:18:21 EDT 2010


to Zope3-Users list after updating subscription email address...

On Wed, 2010-08-25 at 13:06 +0200, Adam GROSZER wrote:
> Hello Edoardo,
> 
> I think the simplest would be to make the upload a
> multipart/form-data, with some headers added if you can.
> Worst case putting the necessary text manually around the binary?
> Then it's a usual POST request what's easily parsed by zope.
> Otherwise it could be really nasty.
> 

You can use a WSGI middleware filter that grabs the request body and
stores it in the WSGI environment before zope consumes it.

Here's a code example:

from webob import Request

class StashPostBody(object):
    """middleware to stash the post body in the WSGI environment"""
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        req = Request(environ)
        # filter more here, if desired
        need_body = req.method == 'POST'
        if need_body:
            req.environ['spb.req_body'] = req.body
        resp = req.get_response(self.app)
        return resp(environ, start_response)
        
def filter_factory(global_conf):
    def filter(application):
        return StashPostBody(application)
    return filter

Once this is configured in the pastescript .ini file as a filter, you
can get the original request body from zope's request object as
request._environ['spb.req_body'].

- Jim Washington





More information about the Zope3-users mailing list