Adding header in GitHub API request in React

602 Views Asked by At

I am using GitHub API to fetch file from repo:

const octokit = new Octokit({
auth: "*******************************",
});

const response = await octokit.request(
  "GET /repos/owner/repo/contents/path",
  {
    owner: "owner",
    repo: "repo",
    path: "path",
  }
);

but I am getting response in base64 format. I need to add header Accept: application/vnd.github.VERSION.raw. Is there a way to add headers in Octokit request?

1

There are 1 best solutions below

1
Ilyas Benhssine On

You can register custom endpoint methods such as octokit.rest.repos.get() using the octokit.registerEndpoints(routes) method

octokit.registerEndpoints({
  foo: {
    bar: {
      method: "GET",
      url: "/repos/{owner}/{repo}/foo",
      headers: {
        accept: "application/vnd.github.VERSION+raw",
      },
      params: {
        ...
      },
    },
  },
});

octokit.foo.bar({
  owner: "octokit",
  repo: "rest.js",
  baz: "quz",
});

view more here Register Custom endpoint | octokit/rest.js documentation