How to extract data from Mailgun webhook headers

I’m dealing with a Mailgun webhook response for delivery notifications and need to retrieve certain information, particularly the subject line from the header data.

The JSON I receive isn’t structured like standard key-value pairs; instead, it uses nested arrays, which complicates the parsing process. I’m looking for a more efficient method to obtain the values I need without having to write numerous loops.

Below is the format of the webhook data I’m working with:

[
  ["Received", "by mail.mailgun.net with SMTP mgrt 9876543210987; Mon, 15 Jun 2023 14:30:15 +0000"],
  ["Content-Type", ["multipart/mixed", {"boundary": "abc123def456ghi789jkl012mno345pqr"}]],
  ["Mime-Version", "1.0"],
  ["Subject", "Welcome to our newsletter"],
  ["From", "John Doe <[email protected]>"],
  ["To", "Jane Smith <[email protected]>"],
  ["Message-Id", "<[email protected]>"],
  ["X-Mailgun-Variables", "{\"campaign_id\": \"summer_2023\", \"user_type\": \"premium\"}"],
  ["Date", "Mon, 15 Jun 2023 14:30:15 +0000"],
  ["Sender", "[email protected]"]
]

What would be the most effective way to extract specific header values from this structure?

just use the dict() constructor - header_dict = dict(webhook_data). since it’s already key-value pairs, you can access it like header_dict['Subject']. skip the comprehensions or fancy stuff. works great with mailgun webhooks.

I’ve dealt with similar webhook parsing nightmares before. The nested array structure is annoying but there’s a cleaner approach.

Skip building your own parser - automate this whole thing. Set up a webhook receiver that transforms Mailgun data into whatever format you need.

I recently handled something similar where we needed to extract multiple header values from various webhook sources. Instead of writing custom parsing logic for each one, I created an automation that:

  1. Receives the webhook
  2. Transforms nested arrays into clean key-value pairs
  3. Extracts specific fields like Subject, From, etc.
  4. Sends cleaned data wherever it needs to go

No need to maintain parsing code or worry about edge cases. The automation handles all data transformation, and you can easily modify what fields to extract without touching code.

For your use case, just configure it to pull out the Subject and any other headers you need, then route that data to your application.

Latenode makes this webhook processing really straightforward - you can visually build the parsing logic and transformations without writing loops or error handling.

To efficiently extract header values from your Mailgun webhook data, converting it into a dictionary format is advisable. In my experience with Mailgun integrations, you can utilize a straightforward mapping function that iterates through the nested array only once. This allows you to create key-value pairs using the first element as the key and the second as the value. Be cautious with headers that have complex structures, like Content-Type, as they may require additional handling. Once you’ve transformed the array into a dictionary, accessing the desired headers becomes a matter of standard key lookups, simplifying the process and resulting in cleaner code.

I’ve used Mailgun webhooks quite a bit, and dictionary comprehension works great here. Since each header is just a two-element array, you can convert everything in one line: headers_dict = {header[0]: header[1] for header in webhook_data}. Then grab the subject with headers_dict.get('Subject'). Watch out for headers like Content-Type though - they can have nested structures, so you might need a type check if you’re processing those. I’ve used this method across several Mailgun projects and it handles high webhook volumes way better than loops.