Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail API - How to Request Only one Name/Value Pair from Email Payload Headers?

How can we capture only the email address in an api response for the gmail API. The fields parameter is set to payload/headers, which returns way more data than we need in the response. All we need is the value from one name/value pair in the JSON response; for example

The full response as we have it now looks something like this

    {
     "payload": {
     "headers": [
                 {
                  "name": "Delivered-To",
                  "value": "xxxxxxx"
                {
                 "name": "Received",
                 "value": "xxxxxxxx"
                },
                {
                "name": "Received-SPF",
                "value": "----"
                 },......
                 {
                  "name": "To",
                  "value": "xxxxxxx" 
                 }, ...... E.T.C........E.T.C ......

  /*All we want is one name/value pair to be returned e.g. */
      {
        "payload": {
                 "headers": [
                   {
               "name": "X-Failed-Recipients",
               "value": "............."
               }
             ]
      }

A better question might be is there a better way to capture bounced/returned mail than this via the gmail API?

Also, is it possible to request an XML response instead of JSON. How can that be done for the gmail API?

Thanks !!

like image 620
user3856348 Avatar asked Jul 19 '14 17:07

user3856348


1 Answers

You can do messages.get(format=METADATA, metadataIncludeHeaders=["To", "From", "Subject"]) for example, now to just request the specific headers you care about . Note this only works with the metadata format (it won't include the body also, if you want all the body you get the full email. Based on the list of headers, shouldn't be too hard to turn that into a map/dict of key => [list, of, values].

As to your second question, yes you can definitely request response in any format you want. That's a standard Google API question though. I can't find a good reference (surely some searching will) but typically you can set an "alt=xml" or "alt=" query parameter to get response in that format. Not sure how this is exposed in any particular client library.

like image 131
Eric D Avatar answered Oct 06 '22 16:10

Eric D