0

So we're busy with an ABP project and struggling to specify the http parameter to map to the app service parameter. Mailgun's Webhook uses "Message-Id" as the http parameter and we are struggling to access that from an app service.

Here is our app service:

    public async Task MailDelivered(int timestamp, string token, string signature, string MessageId)
    {
        var isValid = IsMailGunSignatureValid(timestamp, token, signature);
        if (isValid)
        {
            Console.WriteLine($"Message id: {MessageId}");
            var entity = await communicationEventRepository.FirstOrDefaultAsync(entry => entry.MessageId == $"{MessageId}");
            if (entity != null)
            {
                var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                entity.DeliveredTime = unixEpoch.AddSeconds(timestamp);
                await communicationEventRepository.UpdateAsync(entity);
            }
            else
            {
                throw new UserFriendlyException("Invalid message-id.");
            }
        }
        else
        {
            throw new UserFriendlyException("Invalid signature.");
        }
    }

However, we get a blank string back for MessageId. We cannot name the function parameter Message-Id due to C# naming rules. We also cannot get data from the POST body as JSON, as Mailgun only supports x-www-form-urlencoded for it's webhook calls. Any ideas?

ryan27968
  • 437
  • 3
  • 7
  • 14

1 Answers1

0

You could use a class for the parameters and use the BindProperty attribute to change the name of message-id.

For example:

public class MailDeliveredRequest
{
    public int Timestamp { get; set; }
    public string Token { get;set; }
    public string Signature { get;set; }

    [BindProperty(Name="Message-Id")]
    public string MessageId { get;set; }
}

Then change your method declaration:

public async Task MailDelivered(MailDeliveredRequest msg)
{
....
}

All your query string values are accessible using the msg variable e.g. msg.MessageId etc.

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • Firstly, this changes the app service to only accept a json body(I get 415 unsupported media type when using form input), and secondly, it doesn't even actually map to Message-Id when sending a json body. – ryan27968 May 10 '18 at 22:21
  • I did test it using postman using `x-www-form-urlencoded` and `formdata` types and the web app received the data correctly each time. Can you add an example of the data being sent to the web app? I'm also assuming it is an HttpPost? – Simply Ged May 11 '18 at 07:39