1
Follow a user
One script reply has been approved by the moderators Verified

Note that you'll need to set Content-Length to zero when calling out to this endpoint. For more information, see "HTTP verbs."

Following a user requires the user to be logged in and authenticated with basic auth or OAuth with the user:follow scope.

Created by hugo697 486 days ago Viewed 14830 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 486 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Follow a user
6
 * Note that you'll need to set `Content-Length` to zero when calling out to this endpoint. For more information, see "[HTTP verbs](https://docs.github.com/rest/overview/resources-in-the-rest-api#http-verbs)."
7

8
Following a user requires the user to be logged in and authenticated with basic auth or OAuth with the `user:follow` scope.
9
 */
10
export async function main(auth: Github, username: string) {
11
  const url = new URL(`https://api.github.com/user/following/${username}`);
12

13
  const response = await fetch(url, {
14
    method: "PUT",
15
    headers: {
16
      Authorization: "Bearer " + auth.token,
17
    },
18
    body: undefined,
19
  });
20
  if (!response.ok) {
21
    const text = await response.text();
22
    throw new Error(`${response.status} ${text}`);
23
  }
24
  return await response.text();
25
}
26