Closed proggarapsody closed 1 year ago
the data you posted is indeed a valid JSON.
The thing is, JSON.parse
expects a string as an argument.
But according to the error message, the first argument that you passed is an already parsed Object
, not a string
.
So, somehow, response.data
is already an object, while we expect a string. Maybe you are parsing the data via some interceptor?
Posting the full sample could help :)
@Shaddix I use only interceptor to request which add access token
client.interceptors.request.use(async (config) => {
const token = authStore.getState().getAccessToken()
// eslint-disable-next-line unicorn/prefer-ternary
if (config.method === 'GET' || config.method === 'get') {
config.withCredentials = false
} else {
config.withCredentials = true
}
client.defaults.headers.common.Authorization = `Bearer ${token}`
config.headers.Authorization = `Bearer ${token}`
return config
})
and to response to handle 401 error
client.interceptors.response.use(undefined, async (config) => {
if (config?.response?.status === 401) {
await refreshToken()
client(config.request)
}
})
I don't use any transformResponse functions
@Shaddix In this interceptor I didn't return error reject
client.interceptors.response.use(undefined, async (error) => {
if (error?.response?.status === 401) {
await refreshToken()
client(error.request)
}
return Promise.reject(error.response)
})
This code fixed my problem I close the issue
i don't understand why Json.parse can't parse this object, any thoughts?