Skip to content
Advertisement

How do I add parameters to https get request?

I am working on a little project to learn how to work with APIs by making GET requests to the Twitter API v2 via a node server.

For the get requests I am using Node’s built in https package.

I made a basic GET request that returns a list of the last 10 tweets from a user. I think in order to increase the amount of tweets I can get I have to make a separate parameter object, which I then implement in the get request.

Right now my function looks like this:

function getTweets() {
  const options = {
    host: "api.twitter.com",
    path: `/2/users/${userId}/tweets`,
    headers: {
      authorization:
        `Bearer ${bearerToken}`,
    },
  };

  https
    .get(options, (response) => {
      let data = "";
      response.on("data", (chunk) => {
        data += chunk;
      });

      response.on("end", () => {
        let jsonObject = JSON.parse(data);
        tweetObjects = jsonObject.data;
        tweetObjects.map((item) => {
          let tweetWords = "";
          tweetWords += item.text;
          userTweets.push(tweetWords);
        });
        const result = userTweets.flatMap((str) => str.split(" "));
        console.log(result);
      });
    })
    .on("error", (error) => {
      console.log(error);
    });
}

Right now I only have the options object with host, path, and headers in the request.

This is what I am trying to do:

function getTweets() {
  const options = {
    host: "api.twitter.com",
    path: `/2/users/${userId}/tweets`,
    headers: {
      authorization:
        `Bearer ${bearerToken}`,
    },
  };

  let params = {
    max_results: 100,
  };

  https
    .get(params, options, (response) => {
      let data = "";
      response.on("data", (chunk) => {
        data += chunk;
      });

      response.on("end", () => {
        let jsonObject = JSON.parse(data);
        tweetObjects = jsonObject.data;
        tweetObjects.map((item) => {
          let tweetWords = "";
          tweetWords += item.text;
          userTweets.push(tweetWords);
        });
        const result = userTweets.flatMap((str) => str.split(" "));
        console.log(result);
      });
    })
    .on("error", (error) => {
      console.log(error);
    });
}

But I get

  throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
    at checkListener (events.js:131:11)
    at ClientRequest.once (events.js:496:3)
    at new ClientRequest (_http_client.js:215:10)
    at request (https.js:326:10)
    at Object.get (https.js:330:15)
    at IncomingMessage.emit (events.js:388:22)
    at endReadableNT (internal/streams/readable.js:1336:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'ERR_INVALID_ARG_TYPE'

Advertisement

Answer

You can either only pass the URL or an object containing options as stated in the docs: https://nodejs.org/api/https.html#https_https_get_url_options_callback

So you might want to try something like this:

function getTweets() {
  let params = {
    max_results: 100,
  };

  const options = {
    host: "api.twitter.com",
    path: `/2/users/${userId}/tweets?max_results=${params.max_results}`,
    headers: {
      authorization:
        `Bearer ${bearerToken}`,
    },
  };



  https
    .get(options, (response) => {
      let data = "";

  ...
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement