Search code examples
javascriptreactjshttprequestcsrf

GET request for CSRF not


In my project for back-end I have laravel and for front-end I use react-js
I want to make a get request for CSRF token, on my localhost but the token is not there
this is my postman request postman Headers and this is the response postman response
this is my code for GET request

const getCSRF = async () => {
    const temp = await fetch(`${BaseURL}/csrf`, {
      method: 'GET',
      headers : { 
        'Content-Type': 'application/json',
        'accept':'application/json'
       }
    })
    .then((response) => console.log("CSRF response",response))
}

the console view is Console view


Solution

  • By default, all data fetch request is a GET request and you don't need to pass headers, you just need to convert the response to text(response.text())(as we can see in Postman response)

    const getCSRF = async () => {
        const temp = await fetch(`${BaseURL}/csrf`)
                      .then(response => console.log("CSRF response", response.text()))
    }