question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add global headers with `onRequest` when using $fetch.create

See original GitHub issue

With axios, one can add global headers to config just like below

const instance: AxiosInstance = axios.create({
  baseURL: baseUrl,
  withCredentials: true,
});

// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    const token = ls.getItem("alva.accessToken");
    config.headers = {
      Authorization: `Bearer ${token}`,
    };

    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

5reactions
johannschopplichcommented, Jul 18, 2022

Another example to re-use other headers:

export const useApi = $fetch.create({
  async onRequest({ options }) {
    const authStore = useAuthStore();
    if (!authStore.isLoggedIn) return;
    await authStore.refresh();
    options.headers = new Headers(options.headers);
    options.headers.set("Authorization", `Bearer ${authStore.idToken}`);
  },
});
0reactions
Valar103769commented, Dec 6, 2022
export const useCustomFetch = $fetch.create({
  baseURL: 'http://localhost:4000/',
  async onRequest({ options }) {
    const count = 0;
    options.headers = {
      customs: `count: ${count}`,
    }
  },
})

i think this can overwrite else headers

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetch() does not send headers? - Stack Overflow
I have tried to create Request and specifically add headers using req.headers.append() . The headers still wouldn't send. javascript · http ...
Read more >
How to set request headers in fetch? - RapidAPI
How to set request headers in fetch? · → STEP #1. We need an API that we can call to learn how to...
Read more >
Using Axios to set request headers - LogRocket Blog
In this article, we examined how to set HTTP request headers with Axios by passing an object, creating a specific Axios instance, and...
Read more >
Using the Fetch API - MDN Web Docs
To cause browsers to send a request with credentials included on both same-origin and cross-origin calls, add credentials: 'include' to the init ...
Read more >
unjs/ofetch: A better fetch API. Works on node ... - GitHub
We use conditional exports to detect Node.js and automatically use unjs/node-fetch-native. If globalThis.fetch is available, will be used instead.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found