问题
I'm trying to setup a flask endpoint so that whenever it receives a Gmail push notifications from PubSub, it sends a post request to another API/endpoint.
However upon running the server, the process runs infinitely many times i.e. (receive notification, sends POST request to another API, returns 200 to PubSub)
import Flask
import requests
app = Flask(__name__)
# my endpoint receiving notifications from PubSub
@app.route('/pubsub_notification_receiver')
def receiver():
if request.method == 'POST':
json_body = grab_latest_email()
# send post request to another API
response = requests.post(url, json_body)
print(response.status) # returns OK/200
return 'success'
This leads to infinite notifications from PubSub, and subsequently infinite post requests to the API ie. infinite_loop(receive new PubSub post request, app sends own POST request to API).
Each request is almost instant one after another, so I'm not sure if sending the post request somehow returns a post request that just triggers the receiver again.
If I remove the post request:
if flask.request.method == 'POST':
json_body = grab_latest_email()
# send post request to another API
# response = requests.post(url, json_body)
# print(response.status) # returns OK/200
return 'success'
There's no loop and the PubSub notification works as intended.
Thank you
来源:https://stackoverflow.com/questions/64764967/gmail-pubsub-infinite-push-notifications