developit / developit/redaxios

Response Parsing Error when Status Code is 204 (No Content)

Offen
#96 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
4.9k
Forks
104
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

**Problem:**
Currently, the response from an HTTP request is being parsed, regardless of the status code. This behavior leads to the following error in the browser's debugger menu: `"SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data"` when the status code is 204 (No Content). The error is triggered in Firefox.

**Expected Behavior:**
The parsing of the response data should only occur when the status code is not 204. When the status code is 204, the response should be left unparsed.

**Steps to Reproduce:**
1. Perform an HTTP request that returns a status code of 204.
2. Observe the error in the browser's debugger menu.

**Code Segment:**
```js
return res[options.responseType || 'text']()
.then((data) => {
response.data = data;
// its okay if this fails: response.data will be the unparsed value:
response.data = JSON.parse(data);
})
.catch(Object)
.then(() => {
const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok;
return ok ? response : Promise.reject(response);
});
```

**Possible Solution:**
To resolve this issue, you could modify the code to conditionally parse the response data based on the status code.

**Additional Information:**
- Browser/Environment: `Firefox`
- Redaxios Version: `0.5.1`

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start with the response-handling code segment that calls res[options.responseType || 'text']() and then JSON.parse(data); reproduce an HTTP request returning status 204 in Firefox. Done means a 204 response is left unparsed and no JSON.parse syntax error appears, while other responses retain their existing behavior.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript
Bereich
api
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.