Skip to content

Gmail drafts.create upload always sends application/octet-stream instead of message/rfc822 #276

@rodrigoescandon

Description

@rodrigoescandon

Bug Description

When using gws gmail users drafts create --upload <file.eml>, the uploaded file's MIME type is always set to application/octet-stream, causing the Gmail API to reject it with:

Media type 'application/octet-stream' is not supported.

The Gmail API requires message/rfc822 for draft/message uploads.

Root Cause

In src/executor.rs, build_multipart_body() reads the MIME type from the request body's mimeType field (lines 740-745):

let media_mime = metadata
    .as_ref()
    .and_then(|m| m.get("mimeType"))
    .and_then(|v| v.as_str())
    .unwrap_or("application/octet-stream");

This works for Drive uploads where mimeType is a valid body field (e.g., --json '{"name": "file.pdf", "mimeType": "application/pdf"}').

However, for Gmail uploads, the body schema only allows {"message": {...}, "id": "..."} — there is no mimeType field. Schema validation rejects any attempt to add one:

$ gws gmail users drafts create --params '{"userId": "me"}' \
  --json '{"message": {"threadId": "..."}, "mimeType": "message/rfc822"}' \
  --upload message.eml

Error: Request body failed schema validation:
- mimeType: Unknown property. Valid properties: ["id", "message"]

So the MIME type always falls back to application/octet-stream, making Gmail uploads impossible.

Suggested Fix

When mimeType is not present in the body metadata, the CLI could:

  1. Infer from file extension: .emlmessage/rfc822, .pdfapplication/pdf, etc.
  2. Use API-specific defaults: Gmail endpoints could default to message/rfc822 instead of application/octet-stream.
  3. Add a --upload-type flag: Let users specify the MIME type explicitly (e.g., --upload-type message/rfc822).

Steps to Reproduce

  1. Create a valid RFC 2822 email file (e.g., message.eml)
  2. Run:
    gws gmail users drafts create \
      --params '{"userId": "me"}' \
      --json '{"message": {"threadId": "abc123"}}' \
      --upload message.eml
    
  3. Observe error: Media type 'application/octet-stream' is not supported.

Environment

  • gws version: 0.6.0
  • OS: macOS (Darwin, aarch64)

Affected APIs

This likely affects all Google APIs that require specific upload MIME types and don't have mimeType in their body schema (Gmail being the most notable).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions