drafts.create() fails with "only JSON and multipart supported" error since v6.13.1
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 48/100
Direzione di ricerca
Leggi nylas/handler/http_client.py e il percorso della richiesta utilizzato da drafts.create(), concentrandoti sul passaggio dal parametro json= ai dati serializzati manualmente. Esegui la riproduzione fornita della creazione di una bozza e verifica che le richieste di bozze JSON abbiano esito positivo, mentre il comportamento multipart affrontato da PR #442 rimane intatto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
Draft creation fails with NylasApiError: only JSON and multipart supported when using the Nylas Python SDK v6.13.1+. The Nylas API returns
a 400 error rejecting the draft creation request, even though the request appears to be properly formatted JSON.
Root Cause:
In v6.13.1 (commit d226ff113a62, PR #442), the HTTP client was changed to manually serialize JSON using json.dumps(request_body,
ensure_ascii=False) and pass it via the data= parameter instead of using the json= parameter in requests.request(). While this fixed UTF-8
encoding for multipart requests, it appears to have broken JSON-only requests for draft creation.
The issue is that when using data=json_string instead of json=dict, the requests library doesn't automatically set the correct
Content-Type header, or the manually added application/json; charset=utf-8 header is not being accepted by the Nylas API for draft
creation.
To Reproduce
from nylas import Client
from nylas.models.drafts import CreateDraftRequest
# Initialize client
nylas = Client(api_key="your_api_key", api_uri="https://api.us.nylas.com")
# Attempt to create a draft
draft_request = CreateDraftRequest(
to=[{"email": "[email protected]"}],
subject="Test Subject",
body="Test body content"
)
try:
# This will fail with "only JSON and multipart supported"
response = nylas.drafts.create(
grant_id="your_grant_id",
request_body=draft_request
)
except Exception as e:
print(f"Error: {e}")
# Output: NylasApiError: only JSON and multipart supported
Expected behavior
The draft should be created successfully without errors. The Nylas API should accept the JSON request and return the created draft object.
Workaround:
Bypass the SDK's drafts.create() method and use requests directly with the json= parameter:
import requests
from nylas.models.response import Response
from nylas.models.drafts import Draft
# Get API credentials from the SDK client
api_server = nylas.drafts._http_client.api_server
api_key = nylas.drafts._http_client.api_key
url = f"{api_server}/v3/grants/{grant_id}/drafts"
headers = {
"Authorization": f"Bearer {api_key}",
"X-Nylas-API-Wrapper": "python",
}
# Use requests directly with json= parameter
http_response = requests.post(url, headers=headers, json=draft_request_dict, timeout=30)
if http_response.status_code >= 400:
raise Exception(f"Draft creation failed: {http_response.json()}")
draft_data = http_response.json()
response = Response.from_dict(draft_data, Draft, http_response.headers)
This workaround succeeds because requests.post() with json=dict properly handles Content-Type headers.
SDK Version:
- Broken: v6.13.1 and later (including v6.13.1, v6.14.0, v6.14.1, v6.14.2)
- Last working: v6.13.0 and earlier
Additional context
The breaking change was introduced in commit d226ff113a62 (PR #442) which modified nylas/handler/http_client.py:
- Changed from: requests.request(..., json=request_body, data=data)
- Changed to: requests.request(..., data=json_data or data) where json_data = json.dumps(request_body, ensure_ascii=False)
- Changed Content-Type header from application/json to application/json; charset=utf-8
While PR #442 successfully fixed UTF-8 encoding issues for multipart requests with large attachments, it appears to have broken standard
JSON requests for draft creation. The requests library handles the json= parameter differently than data= with a pre-serialized JSON
string, particularly regarding header management.
Suggested Fix:
Consider using json= parameter for JSON-only requests and data= with manual serialization only for multipart/form-data requests where
UTF-8 encoding is needed.
- Lingua principale
- Python
- Stelle
- 109
- Fork
- 85
- Merge medio
- 8h 41m
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di nylas/nylas-python
-
Attachment downloads ignore request URL and header overridesForse già presa @Kayvan-Zahiri l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 39/100
nylas/nylas-python#491 ·
-
Bug: drafts.create() fails with TypeError for multipart requests (attachments > 3MB)Forse già presa @benjaminwhtan l’ha presa 265 giorni fa. Aperta
nylas/nylas-python#454 · 3 commenti · 1 assegnatario ·
-
UnicodeEncodeError when sending e-mails with some iso-8859 characters (€ symbol)Forse di nuovo libera @pengfeiye l’ha presa 276 giorni fa e non c’è nessuna pull request aperta. Apertabug
nylas/nylas-python#448 · 1 commento · 2 assegnatari ·
-
Create an async version of the clientForse di nuovo libera @AaronDDM l’ha presa 494 giorni fa e non c’è nessuna pull request aperta. Apertaenhancement
nylas/nylas-python#412 · 1 commento · 5 reazioni · 1 assegnatario ·
-
bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
nylas/nylas-python#402 · 1 reazione ·
Tutte le issue di nylas/nylas-python
Issue simili
-
debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
microsoft/vscode-flake8#601 ·
I maintainer di solito rispondono entro 1 giorno
-
nonews refactor
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Uninett/nav#4255 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 3 giorni
-
namespace operations
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 70/100
EclipseFdn/open-vsx.org#14421 ·
I maintainer di solito rispondono entro 1 giorno
-
untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
ubuntu/ubuntu-project-docs#850 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
I maintainer di solito rispondono entro 1 giorno