Search code examples
google-apigmailhttprequestgmail-api

Get more than one header using the metadataHeaders[] query parameter GmailAPI


When I make a GET http request for the metadataHeaders that only requests one of them like so:

https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From

it works just fine. But my question is how do I go about sending the array of headers that I want [to, from, subject], in my request? So basically, how would I restructure my metadataHeaders query parameter... found here ->

https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From

to also contain From, To, & Subject

I have been trying to figure out how to get these headers for quite a while to no avail. I tried looking at the documentation ( https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get ) but although I know its possible thanks to it, I can't seem to find out how to implement it in my http request. I also tried looking through the stack overflow responses to similar questions but many weren't really useful at all since many of the questions were different from mine, using the oauth library, or in a programming language. All I care for is how to make the http request.


Solution

  • Headers is just an array So you can just add it more then once

    metadataHeaders=from&metadataHeaders=to&metadataHeaders=subject

    Request:

    GET https://gmail.googleapis.com/gmail/v1/users/me/messages/185cf8d12166fc7a?format=metadata&metadataHeaders=from&metadataHeaders=to&key=[YOUR_API_KEY] HTTP/1.1
    
    Authorization: Bearer [YOUR_ACCESS_TOKEN]
    Accept: application/json
    

    Resonse:

     "payload": {
        "partId": "",
        "headers": [
          {
            "name": "From",
            "value": "[REDACTED]"
          },
          {
            "name": "To",
            "value": "[REDACTED]@gmail.com"
          },
         {
        "name": "Subject",
        "value": "Security alert"
        },
        ]
      },