gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
if i sent a jwt token from the backend to the frontend and stored it in localstorage,…
- User
if i sent a jwt token from the backend to the frontend and stored it in localstorage, how do i grab it from their and send it to the backend when making api calls
- Assistant
If you have stored a JWT token in the browser's
localStorageand want to send it with your API requests to the backend, typically you include it in the HTTP headers (commonly using theAuthorizationheader with theBearerschema).Here's how you can grab the token from
localStorageand send it with an API call usingfetchoraxios.
Example using
fetch:// Get the token from localStorage const token = localStorage.getItem('jwtToken'); // or whatever key you used // Make an API call with the token in the Authorization header fetch('https://your-backend-api.com/protected-route', { method: 'GET', // or 'POST', 'PUT', etc. headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, // add the token here }, // body: JSON.stringify(data), // if sending data with POST/PUT }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error));