I need some help in replacing fetch with axios here
export const fetchSecret = async (secretName) => {
const token = await getFunctionAppToken();
const headers = {
Authorization: `Bearer ${token}`,
};
const url = `${blbl.GetSecretFromVault}?secretName=${secretName}`;
return Promise.all([fetch(url, { method: "get", headers })])
.then(([response]) => {
const res = response.text();
return res;
})
.catch((error) => {
return Promise.reject(error);
});
};
I am stuck at the line of const res = response.text() for axios.
return Promise.all([axios.get(url, { headers })])
.then(([response]) => {
const res = ??????????
return res;
Axios automatically parses the response body stream as data
, see response schema .
You can also inform Axios to expect a text response via the responseType
option.
Finally, I always preference using the params
option for passing query parameters as it ensures proper URL-encoding.
export const fetchSecret = async (secretName) =>
(await axios.get(blbl.GetSecretFromVault, {
headers: {
Authorization: `Bearer ${await getFunctionAppToken()}`
},
params: { secretName },
responseType: "text"
})).data
I don't know why you were using Promise.all()
so have omitted it.
Your .catch()
was also redundant so it's not included either.
export const fetchSecret = async (secretName) => {
const token = await getFunctionAppToken();
const headers = {
Authorization: `Bearer ${token}`,
};
const url = `${blbl.GetSecretFromVault}?secretName=${secretName}`;
try {
const { data } = await axios(url, { method: 'get', headers })
return data;
}
catch (error) {
return error
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.