I am trying to implement an endpoint to receive email from the mailgun.com API. Basically, when an email is sent to the mailing list, they call your endpoint https://host/messages/ with a POST request.
The problem is that they do not use standard REST and some of the keys contain hyphens. This is an example of the request I receive:
{
'Date': [
'Fri, 26 Apr 2013 11:50:29 -0700'
],
'From': [
'Bob <[email protected]>'
],
'Sender': [
'[email protected]'
],
'Subject': [
'Re: Sample POST request'
],
'To': [
'Alice <[email protected]>'
],
'body-plain': [
'Hi Alice,\n\nThis is Bob.\n\nI also attached a file.\n\nThanks,\nBob\n\nOn 04/26/2013 11:29 AM, Alice wrote:\n> Hi Bob,\n>\n> This is Alice. How are you doing?\n>\n> Thanks,\n> Alice\n\n'
],
I write a serialize and manage to get all fields without hyphens such as From, To, etc. But after hours of trials I cannot manage to get the body-plain.
I tried this:
class MessageSerializer(serializers.ModelSerializer):
Text = serializers.CharField(source='body-plain')
class Meta:
model = Message
fields = ['From', 'To', 'Subject', 'Text']
but I get the error {"Text":["This field is required."]}
It seems to me that Django rest is perhaps modifying the keys. Does someone know how to deal with this problem?
I found a work around that might help others with this issue. Though, I still do not understand why my code in the question does not work. If you have a better way, I am still looking :)
--
The idea is to override the creation on POST in the
ViewSetand bypass theModelSerializer.