Last active
June 17, 2025 00:33
-
-
Save devajmeireles/77951cd44f71da945d0a9de4d675d3bc to your computer and use it in GitHub Desktop.
Laravel Sanctum, Postman Pre-script
This file contains hidden or 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
const csrf = `${pm.environment.get('URL')}/sanctum/csrf-cookie`; | |
pm.sendRequest(csrf, (error, response) => { | |
pm.expect(error).to.equal(null); | |
pm.expect(response).to.have.property('code', 204); | |
pm.expect(response).to.have.property('status', 'No Content'); | |
const xsrfCookie = response.headers.find(header => header.key.toLowerCase() === 'set-cookie' && header.value.includes('XSRF-TOKEN')); | |
if (xsrfCookie) { | |
console.log("XSRF Cookie found:", xsrfCookie); | |
const xsrf = decodeURIComponent(xsrfCookie.value.split(';')[0].split('=')[1]); | |
pm.request.headers.upsert({ | |
key: 'x-xsrf-token', | |
value: xsrf | |
}); | |
pm.environment.set('XSRF-TOKEN', xsrf); | |
} else { | |
console.error("Ops! XSRF Cookie found.") | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment