I have stored a certain data from api response in a state and also as a variable, and when I console log, I can see the response.
Here:
const service = gettoken.then((response) => setId (response.data.services))
console.log(ID)
But when I try to use the state value further, it is not accepting it.
It throws the following error: xhr.js:187 GET https://xxxx.com//xyz 403
GET https://xxxx.com/(this is where the value of const ID should be accepted)/xyz
Any help would be appreciated please. Thanks!
const [tokens, setTokens] = useState("");
const [ID, setId] = useState("");
const [data, setData] = useState("");
var cred = JSON.stringify({ // cred are my valid credentials to get get the token from api
username: "xxxxx",
password: "xxxxx"
});
useEffect(() => {
const showdata = async () => {
getToken().then(token => {
const configuration = {
headers: {
'Authorization': token,
'Content-Type': 'application/json',
}
};
const gettoken = axios.post("https://xxxx.com/auth", cred) // request token
const idtoken = gettoken.then((response) => setTokens(response.data.IdToken))
console.log(tokens)
const service = gettoken.then((response) => setId (response.data.services))
console.log(ID) // this is where I can see the above response from const service
const URL = `https://xxxx.com/${ID}/xyz` // but when I try to use the state overe here in this url, it doesn't accept it.
axios.get(URL, configuration)
.then((response) => {
console.log('Response is', response);
setData(response.data);
})
.catch((error) => {
console.log(error);
});
});
};
showdata();
}, [] );