I have created a Twilio webhook that works OK. However I’ve run into a little problem: sessions don’t seem to work, but cookies do. See example below that contains both versions.
I’m a bit puzzled by this, because sessions are based on cookies if I’m not mistaken.
After any change I have republished the app.
@anvil.server.http_endpoint('/sms')
def sms_reply(**kwargs):
print(kwargs)
# # This works:
# visits = anvil.server.cookies.local.get('visits',0)
# anvil.server.cookies.local['visits']=visits+1
# This does not
visits = anvil.server.session.get("visits", 0)
anvil.server.session['visits']=visits+1
resp = MessagingResponse()
resp.message(f"Welkom terug. Aantal keer eerder geweest: {visits}")
response = anvil.server.HttpResponse(200, str(resp))
response.headers["Content-Type"] = "text/xml"
return response
Anvil’s session functionality has cross-site protection turned on by default, to prevent a common set of attacks known as “XSRF”, or “Cross-site request forgery”.
You can turn this protection off (see the doc link to learn how); just be aware that a malicious website can cause users’ browsers to request API URLs with parameters of their choosing, so don’t trust those requests too much!
In that case, most likely because the session cookies are set to expire quickly/when the browser closes, whereas anvil.server.cookies last longer, and I guess as far as Twilio is concerned those short-term cookies expire between every message.