0

I have this function below that is supposed to fetch data from a Twitch API Endpoint, But I need to pass a query parameter with a key to_id and a value, and I don't know how to pass query parameters using this npm module, I think I have to do this in the gameOptions object below,

(Here's a link of the doc: https://www.npmjs.com/package/request#requestoptions-callback)

function gameRequest(accessToken) {
  setTimeout(() => {
    const gameOptions = {
      url: "https://api.twitch.tv/helix/users/follows",
      method: "GET",
      headers: {
        "Client-ID": MY_CLIENT_ID,
        Authorization: "Bearer " + accessToken,
      },
    };
    if (!accessToken) {
      console.log("No Token");
    } else {
      console.log(gameOptions);

      const gameRequest = request.get(gameOptions, (err, res, body) => {
        if (err) {
          return console.log(err);
        }

        console.log(JSON.parse(body));
      });
    }
  }, 2000);
}
3
  • Try url: "https://api.twitch.tv/helix/users/follows?to_id=" + yourID, Commented Apr 10, 2021 at 12:06
  • thank you, it worked, I was looking for another way but I take this one Commented Apr 10, 2021 at 12:07
  • 1
    Note that this only works when your yourID variable doesn't contain any special characters. Make sure you URL encode the url Commented Apr 10, 2021 at 12:09

1 Answer 1

1

You can user qs in gameOptions :

const gameOptions = {
  url: "https://api.twitch.tv/helix/users/follows",
  method: "GET",
  qs : {
    "to_id" : valueToPass
  },
  headers: {
    "Client-ID": MY_CLIENT_ID,
    Authorization: "Bearer " + accessToken,
  },
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.