-
Notifications
You must be signed in to change notification settings - Fork 61
Fix: #676 Handle text/plain response content type correctly #677
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 5 commits
51d3663
a81db18
7922d34
d867584
fc9c6d3
dbc2713
353f02e
1771c2e
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 |
|---|---|---|
|
|
@@ -15,17 +15,44 @@ limitations under the License. | |
| */ | ||
| import { Cache } from './cache' | ||
|
|
||
| async function DefaultResponseProcess(response: any) { | ||
| /** | ||
| * Process HTTP response with proper content type handling | ||
| * | ||
| * This function handles both JSON and non-JSON responses: | ||
| * 1. For JSON responses, parses and returns the JSON object | ||
| * 2. For non-JSON responses (like text/plain), returns the raw text | ||
| * | ||
| * @param response The fetch API response object | ||
| * @returns Parsed JSON object or raw text content | ||
| */ | ||
| async function DefaultResponseProcess(response: any): Promise<any> { | ||
| if (!response.ok) { | ||
| switch (response.status) { | ||
| case 401: | ||
| throw new Error("Unauthenticated") | ||
| } | ||
|
|
||
| const message = await response.json().then((data: any) => data.message) | ||
| throw new Error(message) | ||
| } else { | ||
| return response.json() | ||
| try { | ||
| const message = await response.json().then((data: any) => data.message) | ||
| throw new Error(message) | ||
| } catch { | ||
| const text = await response.text() | ||
|
||
| throw new Error(text) | ||
| } | ||
| } | ||
|
|
||
| // Get the complete response text first | ||
| const responseText = await response.text(); | ||
|
||
|
|
||
| // Try parsing as JSON, fallback to raw text if failed | ||
| try { | ||
| return JSON.parse(responseText); | ||
| } catch (e) { | ||
| // This is an expected case for non-JSON responses (like text/plain) | ||
| // We intentionally handle this by returning the raw text | ||
| // No need to log as error since this is a valid content type handling | ||
| console.debug("Response is not JSON, handling as plain text"); | ||
| return responseText; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -754,7 +781,12 @@ function GetTestCaseAllHistory(req: TestCase, | |
| .then(callback).catch(errHandle) | ||
| } | ||
|
|
||
| function DownloadResponseFile(testcase, | ||
| interface ResponseFile { | ||
| body: string; | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| function DownloadResponseFile(testcase: ResponseFile, | ||
| callback: (d: any) => void, errHandle?: (e: any) => void | null) { | ||
| const requestOptions = { | ||
| method: 'POST', | ||
|
|
||
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.
Type 'null' is not assignable to type '{}'.ts-plugin(2322)
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.
You can check the content type, then handle JSON or not.