From ea696ba044bb96c2b9a9342dc625c4a8c9aa0009 Mon Sep 17 00:00:00 2001 From: Lucas Schoenfelder <les17@inf.ufpr.br> Date: Mon, 8 Feb 2021 21:22:48 -0300 Subject: [PATCH] fixed incorrect JSON parsing error --- .../HelperFunctions/getAxiosConfig.js | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Components/HelperFunctions/getAxiosConfig.js b/src/Components/HelperFunctions/getAxiosConfig.js index 04dd3606..7841d441 100644 --- a/src/Components/HelperFunctions/getAxiosConfig.js +++ b/src/Components/HelperFunctions/getAxiosConfig.js @@ -33,7 +33,6 @@ function fetchHeaders () { if (auth_headers) { const myHeaders = new Headers(auth_headers) - myHeaders.append('Content-Type', 'application/json') return myHeaders } else { @@ -88,7 +87,9 @@ export const getRequest = (url, onSuccess, onError) => { if (response.headers.has('access-token')) { updateAccessToken(response.headers.get('access-token')) } - return response.json() + return response.json().catch(err => { + return {}; + }) }) .then(data => { console.log(data) @@ -108,7 +109,9 @@ export const deleteRequest = (url, onSuccess, onError) => { if (response.headers.has('access-token')) { updateAccessToken(response.headers.get('access-token')) } - return response.json() + return response.json().catch(err => { + return {}; + }) }) .then(data => { console.log(data) @@ -120,16 +123,26 @@ export const deleteRequest = (url, onSuccess, onError) => { } export const putRequest = (url, payload, onSuccess, onError) => { + let newHeaders = fetchHeaders() + if (payload instanceof FormData) { + newHeaders.append('Content-Type', 'multipart/form-data') + } + else { + newHeaders.append('Content-Type', 'application/json') + } + fetch((`${apiUrl}${url}`), { method : 'PUT', - headers : fetchHeaders(), - body: JSON.stringify(payload) + headers : newHeaders, + body: payload instanceof FormData ? payload : JSON.stringify(payload) }) .then(response => { if (response.headers.has('access-token')) { updateAccessToken(response.headers.get('access-token')) } - return response.json() + return response.json().catch(err => { + return {}; + }) }) .then(data => { console.log(data) @@ -141,16 +154,26 @@ export const putRequest = (url, payload, onSuccess, onError) => { } export const postRequest = (url, payload, onSuccess, onError) => { + let newHeaders = fetchHeaders() + if (payload instanceof FormData) { + newHeaders.append('Content-Type', 'multipart/form-data') + } + else { + newHeaders.append('Content-Type', 'application/json') + } + fetch((`${apiUrl}${url}`), { method : 'POST', - headers : fetchHeaders(), - body: JSON.stringify(payload) + headers : newHeaders, + body: payload instanceof FormData ? payload : JSON.stringify(payload) }) .then(response => { if (response.headers.has('access-token')) { updateAccessToken(response.headers.get('access-token')) } - return response.json() + return response.json().catch(err => { + return {}; + }) }) .then(data => { console.log(data) @@ -172,7 +195,9 @@ export const fetchAllRequest = (urls, onSuccess, onError) => { updateAccessToken(res.headers.get('access-token')) } } - return Promise.all(responses.map( (response) => response.json())) + return Promise.all(responses.map( (response) => response.json().catch(err => { + return {}; + }))) }).then( (data) => { onSuccess(data) }).catch((error) => { @@ -217,7 +242,9 @@ export const authentication = (url, payload, onSuccess, onError) => { sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers)) - return response.json() + return response.json().catch(err => { + return {}; + }) }) .then(data => { console.log(data) -- GitLab