0

I'm trying to use Hacker News API , i am able to call the API and make the request to fetch the stories. As per their API documentation the source URL must contain the ID(required) to access the stories' content, I am getting the correct URL in the console and clicking on the URL gives the appropriate JSON but beyond that I am unable to use/call the JSON content like author name,title etc.

JS CODE:

var records;
var userRecords;
var fileJson;
var startRequest = new XMLHttpRequest();
var sourceUrl = "https://hacker-news.firebaseio.com/v0/";
var sourceUrlAdd = "showstories.json";
var finalURL = sourceUrl + sourceUrlAdd;

startRequest.open("GET",finalURL);
startRequest.onload = function() {
    fileJson = JSON.parse(startRequest.responseText);
    for(var i = 0 ; i < fileJson.length; i++) {
        userRecords = sourceUrl + fileJson[i] + ".json";
    }
}
startRequest.send();

How do I make it work??

Zoe
  • 27,060
  • 21
  • 118
  • 148
ankitjt
  • 477
  • 1
  • 5
  • 15
  • Have you tried displaying the content of responseText? Do you know that it is encoded as JSON? I just opened the URL in a browser, it isn't JSON, its HTML, so you JSON.parse will not be successful. – SPlatten Dec 06 '17 at 06:54
  • The page opens in JSON format text, the samples in the documentation are referring to a JSON file, either way, i am still unable to reflect the ID fetched from the first request. – ankitjt Dec 06 '17 at 06:58
  • Ok, apologies, now that I've appended showstories.json to the URI it does open a JSON feed, an array of long integers. – SPlatten Dec 06 '17 at 07:02
  • no worries, i was just about to send you the link , see the problem now? – ankitjt Dec 06 '17 at 07:03
  • I've tried opening 'https://hacker-news.firebaseio.com/v0/15853345.json' it doesn't seem to be valid, are you sure this is how the file is formatted? – SPlatten Dec 06 '17 at 07:06
  • You might find this helpful, looks like your URL is incomplete: https://github.com/HackerNews/API, append item to the URL before the ID and ext. – SPlatten Dec 06 '17 at 07:10
  • i following their documentation – ankitjt Dec 06 '17 at 07:21
  • then there documentation must be incorrect or you have misinterpreted it, the URL as I have posted now works and returns JSON...try it! – SPlatten Dec 06 '17 at 07:23
  • ok, let me give that a shot, thanks a lot – ankitjt Dec 06 '17 at 07:23

1 Answers1

2

Looking at the information on: GitHub

Your URL in the loop should be modified to:

    for(var i = 0 ; i < fileJson.length; i++) {
       userRecords = sourceUrl + "/item/" + fileJson[i] + ".json";
    }

Your code doesn't look complete as the loop will just overwrite userRecords for every iteration, if you want to store them:

    userRecords = [];
    for(var i = 0 ; i < fileJson.length; i++) {
       userRecords.push(sourceUrl + "/item/" + fileJson[i] + ".json");
    }

The above will give you an array populated with all the results.

For example this is the content from the first item in the userRecords array:

    {"by":"ntrippar"
    ,"descendants":45
    ,"id":15853345
    ,"kids"[15858055,15854752,15856479,15854664,15858253,15856298,15854305,15855332,15858118,15857061,15854116]
    ,"score":159
    ,"time":1512494533
    ,"title":"Show HN: SeKey: An SSH Agent for OS X, Using Secure Enclave and TouchID, in Rust"
    ,"type":"story"
    ,"url":"https://github.com/ntrippar/sekey"}

If you parse the above it will translate into an object.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • This I was able to achieve, but the thing is that after that I am unable to access the other details e.g after running the loop it returns a URL i'm doing this in the console, and when i click on one of the URL it produces the JSON formatted file but say if I want to know just the names of all the users who posted , this is something I'm unable to filter out. – ankitjt Dec 06 '17 at 07:29
  • This is another question, but basically you need to do the same thing again, open each JSON URL you have in the userRecords array and parse the JSON again, then you will get the object content. I'll edit my answer to include a sample of the return JSON. – SPlatten Dec 06 '17 at 07:32