Skip to content

Instantly share code, notes, and snippets.

@uyriq
Created October 5, 2023 07:00
Show Gist options
  • Save uyriq/115c9ea40509e7faa6911653a087d35d to your computer and use it in GitHub Desktop.
Save uyriq/115c9ea40509e7faa6911653a087d35d to your computer and use it in GitHub Desktop.
Axios req
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