diff --git a/src/Components/HelperFunctions/getAxiosConfig.js b/src/Components/HelperFunctions/getAxiosConfig.js index 04dd360683bd7311b3c30322332dea3ed6864797..7841d441bbf4c7085d8b965fe76f0d17863dbf99 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)