Fixed axios parsing JSON for non-JSON content types
This commit is contained in:
parent
a0da79165d
commit
c2a919e178
2 changed files with 24 additions and 2 deletions
|
|
@ -9,7 +9,29 @@ const axiosWithProxy = async (req, { state }) => {
|
|||
}
|
||||
|
||||
const axiosWithoutProxy = async (req, _store) => {
|
||||
const res = await axios(req)
|
||||
const res = await axios({
|
||||
...req,
|
||||
transformResponse: [
|
||||
(data, headers) => {
|
||||
// If the response has a JSON content type, try parsing it
|
||||
if (
|
||||
headers["content-type"].startsWith("application/json") ||
|
||||
headers["content-type"].startsWith("application/vnd.api+json") ||
|
||||
headers["content-type"].startsWith("application/hal+json")
|
||||
) {
|
||||
try {
|
||||
const jsonData = JSON.parse(data)
|
||||
return jsonData
|
||||
} catch (e) {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// Else return the string itself without any transformations
|
||||
return data
|
||||
},
|
||||
],
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
|
|
|
|||
2
package-lock.json
generated
2
package-lock.json
generated
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "postwoman",
|
||||
"version": "1.9.5",
|
||||
"version": "1.9.7",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue