Last active
March 17, 2017 15:57
-
-
Save sspross/39f5c4b4b752bb275a44cc22b2bd10a8 to your computer and use it in GitHub Desktop.
how to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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