Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass Github Auth token in Node Fetch?

I’m currently dealing with API rate:

API rate limit exceeded. 
(But here's the good news: 
Authenticated requests get a higher rate limit. Check out the documentation for more details.)

I’ve tried multiple different methods, but seems like I’m missing something, here is my current state:

const TOKEN = process.env.GITHUB_TOKEN;

  try {
    return await fetch(
      url +
        new URLSearchParams({
          method: "GET",
          headers: {
            Authorization: `Bearer ${TOKEN}`,
          },
        })
    ).then((res) => res.json());
  } catch (err) {
    console.log("Error at Get Pulls", err);
  }

I’ve tried with Bearer, without, with lowercase, but at the end I always get the same error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are passing the headers and method inside the URLSearchParams, which is wrong. You must pass these properties to the fetch method.

URLSearchParams is a helper to work with the query string of a URL.

So do this instead

await fetch(url, {
    method: "GET",
    headers: {
        Authorization: `Bearer ${TOKEN}`,
    },
}).then((res) => res.json());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading