https://api.i-as.dev/api/csrf/
/token
{
"token": "<csrf-token>"
}
GET /token
curl -X GET /token
/secure
Content-Type: application/json
x-csrf-token: <csrf-token>
{
"key": "value"
}
{
"message": "Data received"
}
POST /secure
Content-Type: application/json
x-csrf-token: <csrf-token>
{
"key": "value"
}
curl -X POST /secure \
-H "Content-Type: application/json" \
-H "x-csrf-token: {csrf-token}" \
-d '{"key": "value"}'
Expired Token:
{
"error": "Token expired"
}
Invalid Token:
{
"error": "Invalid token"
}
// Get CSRF token
async function getCsrfToken() {
try {
const response = await fetch('/token');
const data = await response.json();
return data.token;
} catch (error) {
console.error('Error fetching CSRF token:', error);
}
}
// Send data with CSRF token
async function sendData() {
const token = await getCsrfToken();
if (token) {
try {
const response = await fetch('/secure', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-csrf-token': token
},
body: JSON.stringify({ key: 'value' })
});
const data = await response.json();
console.log('Response:', data);
} catch (error) {
console.error('Error sending data:', error);
}
}
}
sendData();