Alex
Alex

Reputation: 1999

Get Message Body and Headers In One API Call Gmail API (Python)

Is there a way to get the email headers and email body in one API call?

I can do it in two calls:

headers = service.users().messages().get(userId='me', id=message['id'], format='metadata').execute()

msg = service.users().messages().get(userId='me', id=message['id']).execute()

but it would be faster if I could do it in one call.

Any ideas?

Upvotes: 0

Views: 4459

Answers (2)

Tase
Tase

Reputation: 1

The headers and the body will only be present in a message when using the FULL format. The RAW and MINIMAL format will omit the headers, and the METADATA format will omit the body.

Your second call should include both the body and the headers.

You can test the API here: https://developers.google.com/gmail/api/v1/reference/users/messages/get?authuser=1

Upvotes: 0

payne
payne

Reputation: 14187

user.messages.get() will return everything, including headers, if you set the format argument to "full" or "raw".

See: https://developers.google.com/gmail/api/v1/reference/users/messages/get

Acceptable values are:

"full": Returns the full email message data with body content parsed in the payload field; the raw field is not used. (default)

"metadata": Returns only email message ID, labels, and email headers.

"minimal": Returns only email message ID and labels; does not return the email headers, body, or payload.

"raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used.

Also: https://developers.google.com/gmail/api/v1/reference/users/messages

The headers are returned in the payload.headers field.

Upvotes: 1

Related Questions