How to add JWT to authorization header?

How to add JWT to authorization header?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing JWT Authorization Headers in Stateful Workflows

When a server issues a JSON Web Token (JWT) upon successful authentication, the handoff isn't complete until the client adopts that token for subsequent requests. The standard mechanism for this is appending the token to the Authorization header using the Bearer scheme. However, the specific implementation details often depend on how you are managing session state and whether your backend relies on cookies or pure headers for persistence.

In your scenario, the login flow involves a POST request with credentials, followed by the server generating a JWT and saving it to a cookie. This hybrid approach—issuing a token while maintaining a cookie—is common in frameworks like Laravel, but it introduces a specific nuance regarding how you must configure the client for subsequent requests.

Configuring the Authorization Header

To send the JWT back to the server, you cannot simply rely on the cookie being sent automatically if your API endpoints are configured to expect headers instead of cookies for validation. The standard HTTP header format requires the string Bearer <token_value>.

For a web browser using JavaScript (e.g., Fetch API or Axios), this configuration is handled in the request options:

const response = await fetch('/api/protected-route', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({ data: 'some payload' })
});

If you are using Axios, the setup is often more concise, allowing you to set a default header so every request inherits the token:

axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

It is critical to ensure that the token retrieved from the cookie during the login response is extracted correctly. If your server saves the JWT in a secure, HttpOnly cookie, you must read that specific cookie value from the client's storage before constructing the header string. Attempting to send a header without populating it first will result in a 401 Unauthorized error at the next request, as the server cannot validate an empty or missing token.

Handling Data Submission for Background Processes

Your second question touches upon a common architectural ambiguity: how to separate identity from payload when initiating long-running tasks or calculations. The short answer is that you send a single JWT containing your identity, and you attach the process-specific data to the request body of every subsequent call.

You do not need a second JWT for the calculation data. The JWT's sole purpose in this context is authentication (who am I?) and potentially authorization (what can I do?). The calculation data belongs in the request payload.

Consider a workflow where a user triggers an export process:

  1. Login: You receive token_abc saved in a cookie.
  2. Trigger Calculation: You send a POST request with headers containing Authorization: Bearer token_abc and a body containing { "type": "export", "id": 123 }.
  3. Process Result: The server validates the header, identifies the user, processes the calculation, and returns the result in the response body.

If you were to generate a second JWT for the data, you would be decoupling the data from the identity unnecessarily. This creates complexity in token expiration management and storage without adding security benefits. The standard pattern is one token per session (or per user), with all data variations occurring in the HTTP method or body.

Trade-offs in Cookie vs. Header Management

Your current architecture saves the JWT in a cookie. While convenient for browser-based clients, this creates a potential divergence between stateless API designs and stateful ones. If your backend is designed to be purely stateless (verifying tokens via the signature rather than reading session cookies), you must ensure your frontend logic explicitly reads the cookie upon login and then constructs the Authorization header manually.

Failing to do this results in a mismatch where the browser sends the token silently via the cookie, but the server expects it in the header. Conversely, if the server is configured to read from the cookie for validation, adding a duplicate header might be redundant or even cause conflicts depending on your framework's middleware configuration.

For robust API development, especially when integrating with services like those discussed at https://laravelcompany.com, it is often recommended to configure CORS policies that allow credentials and ensure the Authorization header is explicitly managed in the client layer. This ensures that even if a server-side session cookie exists, the explicit presence of the token in the header guarantees that the request is intended for an authenticated resource, regardless of how the backend chooses to validate it.

Our network