Commit dbc2713
committed
fix: Refine response handling based on Content-Type
Refactored `DefaultResponseProcess` to intelligently handle HTTP responses:
1. Explicitly checks the 'Content-Type' header.
2. If 'text/plain', directly processes the response as text using `response.text()`.
3. For other content types, attempts to parse as JSON using `response.json()`.
4. If JSON parsing fails (e.g., the content is not valid JSON despite the
header, or it's another non-JSON type like XML), the response is
cloned (`response.clone()`) before reading its body as text. This
correctly handles scenarios where the initial `response.json()` call
has already consumed the response body stream.
This approach ensures the response body is consumed only once per effective read attempt (once for text, or once for JSON attempt + once for text fallback on a cloned response if needed), resolving concerns about potential multiple reads on the original response stream and improving robustness in parsing diverse response types.1 parent fc9c6d3 commit dbc2713
1 file changed
+13
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
45 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
46 | 50 | | |
47 | | - | |
| 51 | + | |
48 | 52 | | |
49 | | - | |
| 53 | + | |
50 | 54 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | 61 | | |
58 | 62 | | |
| |||
0 commit comments