Commit 1ff4115
authored
fix(fetch): preserve Content-Type header for SDK requests on Node.js (#276)
## Problem
`sentry issue explain` and `sentry issue plan` were failing with `Error:
Unsupported media type "" in request.` (HTTP 415) exclusively on the
Node.js runtime (the npm package). Bun builds were unaffected.
Tracked as CLI-65, affecting 2 users across 9 occurrences.
## Root Cause
The `@sentry/api` SDK constructs a `Request` object with `Content-Type:
application/json` in its headers, then calls `_fetch(request2)` with a
single argument (no `init`). Our `authenticatedFetch` received `init =
undefined`, so `prepareHeaders` created `new Headers(undefined)` — empty
headers — losing the SDK's `Content-Type`.
`fetchWithTimeout` then called `fetch(request, { ...init, headers,
signal })`. Per the Fetch spec, passing an `init` with `headers` to
`fetch(Request, init)` **replaces** the Request's headers entirely. The
server received a POST with no `Content-Type` and returned HTTP 415.
Bun merges headers from the Request object in this scenario rather than
replacing them, masking the bug there.
## Fix
In `prepareHeaders`, fall back to the `Request` object's headers when
`init` is undefined:
```ts
const sourceHeaders =
init?.headers ?? (input instanceof Request ? input.headers : undefined);
```
Also corrects method extraction for HTTP span tracing when the SDK
passes only a `Request` object (was always recording `GET`, now
correctly records the Request's method).1 parent 0a8e649 commit 1ff4115
1 file changed
+20
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
57 | | - | |
58 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
59 | 74 | | |
60 | 75 | | |
61 | 76 | | |
| |||
211 | 226 | | |
212 | 227 | | |
213 | 228 | | |
214 | | - | |
| 229 | + | |
| 230 | + | |
215 | 231 | | |
216 | 232 | | |
217 | 233 | | |
218 | 234 | | |
219 | | - | |
| 235 | + | |
220 | 236 | | |
221 | 237 | | |
222 | 238 | | |
| |||
0 commit comments