0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.