Search code examples
sendgridf5

Redirect SendGrid Webhook Events with an F5 using Custom Headers


When sending an email through SendGrid I am sending a custom header. Is there a way to configure the webhooks to get the custom header back from SendGrid so that a F5 iRule can be easily written to redirect the traffic based on the value in this custom header. I know I can use .addCustomArgs(...) to return custom data but I would like the custom data in the header.

var client = new SendGridClient("API_KEY");
var from = new EmailAddress("[email protected]", "Example User");
var subject = "Testing with SendGrid API";
var to = new EmailAddress("[email protected]", "Example User");
var plainTextContent = "Test Content";
var htmlContent = "<strong>Testing with HTML content</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var identifiers = new Dictionary<String, String>();
identifiers["application"] = "APP_NAME_GOES_HERE";
identifiers["resource"] = "RESOURCE_NAME_GOES_HERE";    
msg.AddHeaders(identifiers);   
var response = await client.SendEmailAsync(msg);

Solution

  • Twilio SendGrid developer evangelist here.

    I'm afraid you cannot set headers for the SendGrid event webhook to send back. Custom arguments are sent as part of the JSON body.

    I've not used F5 iRules before, but it seems that you may be able to parse and use the JSON body of a request within an iRule. According to this post, you can use iRules LX to deal with the JSON using JavaScript.

    Or it seems that you can combine HTTP::collect with an HTTP_REQUEST_DATA block to collect and do things with the request body.