Skip to content

Instantly share code, notes, and snippets.

@sspross
Last active March 17, 2017 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspross/39f5c4b4b752bb275a44cc22b2bd10a8 to your computer and use it in GitHub Desktop.
Save sspross/39f5c4b4b752bb275a44cc22b2bd10a8 to your computer and use it in GitHub Desktop.
how to
from django.views.generic import View
from django.http import QueryDict
import json
class Webhook(View):
def post(self, request):
"""
Explaining the problem if sendgrid sends the `text` content
in a different charset then 'utf-8'.
See sendgrid documentation here:
https://sendgrid.com/docs/API_Reference/Webhooks/inbound_email.html#-Character-Sets-and-Header-Decoding
"""
charsets_info = request.POST.get('charsets')
charsets = json.loads(charsets_info)
charset = charsets.get('text')
if charset.lower() not in ['utf-8', 'utf8']:
# I know, this don't work, but how would something
# like this be possible in Django?
new_post = QueryDict(request.body, encoding=charset)
text = new_post.get('text')
else:
text = request.POST.get('text')
# go on and work with text...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment