Skip to content

Instantly share code, notes, and snippets.

@virolea
Created October 23, 2019 09:40
Show Gist options
  • Save virolea/5edba91068ce7d6a824b1e5e2dc7ef7c to your computer and use it in GitHub Desktop.
Save virolea/5edba91068ce7d6a824b1e5e2dc7ef7c to your computer and use it in GitHub Desktop.
Configure axios to automatically set the Rails CSRF token
import axios from 'axios'
const tokenEl = document.getElementsByName('csrf-token')[0]
if (tokenEl) {
const token = tokenEl.getAttribute('content')
axios.defaults.headers.common['X-CSRF-Token'] = token
}
axios.defaults.headers.common['Accept'] = 'application/json'
axios.defaults.headers.post['Content-Type'] = 'application/json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment