Skip to content
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

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

Open
jopms opened this issue Oct 12, 2023 · 1 comment
Open

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

jopms opened this issue Oct 12, 2023 · 1 comment

Comments

@jopms
Copy link

jopms commented Oct 12, 2023

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:

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
@landsman
Copy link

landsman commented Feb 21, 2024

did you tried to write interceptor for that?

function ResponseInterceptor(response: AxiosResponse) {
	if (response.status === 204) {
		response.data = null
	}

	return response
}

async function ResponseErrorInterceptor(error: AxiosError) {
	return Promise.reject(error)
}

axios.interceptors.response.use(ResponseInterceptor, ResponseErrorInterceptor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants