-
Notifications
You must be signed in to change notification settings - Fork 198
fix: add content-type check for non-JSON responses in fetchExternal #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,10 @@ class HttpClient { | |||||||||||
| if (!response.ok) { | ||||||||||||
| throw new Error(`${config.method || 'GET'} ${url} failed: ${response.statusText}`); | ||||||||||||
| } | ||||||||||||
| const contentType = response.headers.get('content-type') || ''; | ||||||||||||
| if (!contentType.includes('application/json')) { | ||||||||||||
|
||||||||||||
| if (!contentType.includes('application/json')) { | |
| const normalizedContentType = contentType.toLowerCase(); | |
| const mediaType = normalizedContentType.split(';', 1)[0].trim(); | |
| const isJsonResponse = mediaType === 'application/json' || mediaType.endsWith('+json'); | |
| if (!isJsonResponse) { |
Copilot
AI
Apr 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description says the HttpClient now ensures all HTTP responses are JSON before parsing, but this validation is only added to fetchExternal(); the core request() path still calls response.json() unconditionally. Either broaden the check to request() as well or adjust the PR description to match the actual behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message can be unhelpful when the server omits the Content-Type header (you'll end up with
returned non-JSON response ()). Consider distinguishing between a missing header and a non-JSON header value so the thrown error is actionable during debugging.