-1

I am trying to access all the projects from Harvest API and parse it as a JSON file. But I am new to Node JS so I do not know where to begin. Here is the link to the API documentation: Harvest API Documentation

API requires all the calls to be authenticated how can I work around that?

Thank you in Advance

Biswadev
  • 1,456
  • 11
  • 24
Harman Pannu
  • 25
  • 11
  • 1
    To convert JSON string to an object. You can use `JSON.parse({JSON_STRING_RECEIVED_FROM_API})`. And you can use `request` node module to fetch the data by making a HTTP request. But be aware, request is async. – Leo Li Apr 25 '18 at 22:46
  • Possible duplicate of [Parse JSON in JavaScript?](https://stackoverflow.com/questions/4935632/parse-json-in-javascript) – Leo Li Apr 25 '18 at 22:48
  • @LeoLi parsing the JSON string is just the cherry of the cake. He have to get it first. – ibrahim mahrir Apr 25 '18 at 22:50
  • 1
    @ibrahimmahrir That is why I said he should use the `request` module... And he should look [doc](https://github.com/request/request) first... – Leo Li Apr 25 '18 at 22:53
  • 1
    The `request` module will auto parse the json if you set the json option. However IMHO the request module isn't ideal since the new browser fetch api is both promise native and standard. `fetch(url).then(r => r.json()).then(parsed => console.log(parsed))` – generalhenry Apr 25 '18 at 22:59
  • 1
    @generalhenry The fetch API is another choice, but I have to remind you the OP is asking in JS run-time, not a browser. So the native `window.fetch` is not there, although there is another module call `node-fetch` which allows you to use `fetch` API in node.js. But anyway, you need to lode the module. – Leo Li Apr 26 '18 at 01:16
  • Or easier, there's a node package for that https://www.npmjs.com/package/harvest – generalhenry Apr 26 '18 at 02:13
  • I am able to fetch the data now using harvest package. How can I turn into JSON file – Harman Pannu Apr 26 '18 at 03:47

1 Answers1

1

You can parse using JSON.parse(data) to get the JSON Object

const https = require("https");

const options = {
  protocol: "https:",
  hostname: "api.harvestapp.com",
  path: "/v2/users/me",
  headers: {
    "User-Agent": "Node.js Harvest API Sample",
    Authorization: "Bearer " + process.env.HARVEST_ACCESS_TOKEN,
    "Harvest-Account-ID": process.env.HARVEST_ACCOUNT_ID,
  },
};

https
  .get(options, (res) => {
    const { statusCode } = res;

    if (statusCode !== 200) {
      console.error(`Request failed with status: ${statusCode}`);
      return;
    }

    res.setEncoding("utf8");
    let rawData = "";
    res.on("data", (chunk) => {
      rawData += chunk;
    });
    res.on("end", () => {
      try {
        const parsedData = JSON.parse(rawData);
        console.log(parsedData);
      } catch (e) {
        console.error(e.message);
      }
    });
  })
  .on("error", (e) => {
    console.error(`Got error: ${e.message}`);
  });

Please refer

https://github.com/harvesthq/harvest_api_samples/blob/master/v2/harvest_api_sample.js

Dominik
  • 6,078
  • 8
  • 37
  • 61
Biswadev
  • 1,456
  • 11
  • 24