Created
October 5, 2023 07:00
-
-
Save uyriq/115c9ea40509e7faa6911653a087d35d to your computer and use it in GitHub Desktop.
Axios req
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function Page() { | |
const [token, setToken] = useState(''); | |
// function that do post request to get token from backend localhost:8000/api/v1/users/token using axios and post method body email and password | |
function handToken() { | |
try { | |
const data = { | |
email: 'prostome2@prosto.me', | |
password: '222', | |
}; | |
const config = { | |
method: 'POST', | |
mode: 'no-cors', | |
maxBodyLength: Infinity, | |
url: 'http://localhost:8000/api/v1/users/token', | |
// add headers 'Access-Control-Allow-Origin', '*' | |
headers: { | |
// 'Access-Control-Allow-Origin': '*', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
// set refferer to localhost:3000 | |
// 'refferer': 'http://localhost:3000', | |
}, | |
data: qs.stringify(data), | |
}; | |
axios(config) | |
.then((response) => { | |
setToken(response.data.token); | |
console.log(`handToken - ${response.status}`); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
} catch (error) { | |
console.log(error); | |
} finally { | |
console.log(token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment