Question

I receive posted data and immediately return an empty 200 OK response. After that I will process the received data. I'm considering how to do it with a teardown function but I didn't find how to pass it the received data:

@app.route('/f', methods = ['POST'])
def f():

    data = request.stream.read()
    return ''

@app.teardown_request
def teardwon_request(exception=None):

    # How to use posted data here?

Flask version is 0.10.1

I'm trying to implement a Paypal IPN listener

https://developer.paypal.com/webapps/developer/docs/classic/ipn/gs_IPN/#overview

Notice that the listener's HTTP 200 response happens before the listener's IPN message.

Was it helpful?

Solution

You are overcomplicating things; just send a request from your Flask server in the request handler. Paypal IPN notifications just require a empty 200 response, Paypal does not mandate that you send the 200 OK before you can send the HTTP request to their servers.

The overview page is indeed confusing, but the PHP code posted won't close the request until the Paypal IPN post back to their server has completed either.

If this was a hard requirement (making this a terrible design), you'd have to handle the request back to Paypal asynchronously. You can do this with a separate thread, for example, using a queue, push in the data you received from the IPN, and have a separate thread poll the queue and communicate to Paypal from that thread. Or you could use Celery to simplify the job (push a task out to be handled asynchronously). Either way, this would let you close the incoming request early.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top