Send CSRF Token Laravel by Node.js - CSRF Token Mismatch Error

Asked

Viewed 25 times

0

Good night!

I have an API that was developed in Laravel, and when I am consuming it in NODE.JS with Axios and is returning the CSRF Mismatch Token error

So I believe I need to send the Token through the Node, but I’m not finding a way to do it.

Below my code that is consuming the API

axios.post('http://127.0.0.1:8000/api/v1/messages', {
    message: 'oi, isso teste',
    id:11
},{
    
username: 'admin',
password:'admin'
    
}).then((res) => {
    console.log(`statusCode: ${res.statusCode}`)
    console.log(res)
}).catch((error) => {
    console.error(error)
})

1 answer

0

Hello,

I have the working environment with Laravel as API and front with Reactjs, suddenly work for you.

Just put headers with Authorization and pass the token.

I did it this way...

useEffect(()=>{
   api.get('api/editoras', {
      headers: { 
          Authorization: `Bearer ${token}` 
      }
   }).then(response=>{
       if(response.data.status){
          alert('Sessão expirado');
          history.push('/');
       }
         setEditoras(response.data);
       });
},[]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.