简体   繁体   中英

How can I get json from an API url on node.js?

So I'm trying to get text from a URL, it is an api url but it doesn't have any special arguments, I can use a static link. So if I put the link into the browser, its a blank page with just this text:

{"id":"hFXuavIS2Fu5TJs9WC9M-ylckZ-o4b21inxr5h8axKtT1HSH","accountId":"TIIQ3c3YnJc96-p4ASIt7spCeICawEWiBJKwwDYDm0WUP23TlGoum2cc","puuid":"I5Lb36kGyhZTq3ypf1fqh6tk3p3xVA8-l-6_EDA4a9imdXm_uXAvVs-Prc8hkRoKzGVT0vr55kZ1lQ","name":"CH0G4TH","profileIconId":949,"revisionDate":1588735966000,"summonerLevel":49}

Or different depending on the username.

I've tried using jquery for node, but it needs jsdom and I cannot get it to work at all, I get an error of Error: Cannot find module './jsdom/living/generated/utils.js' .

And using XMLHttpRequest, I don't get it. Sites show for calling for something specific inside the json, but I don't want that, just the whole thing, so skipping that and going directly to.send, it returns undefined.

This is what I've copied down from a site:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
        var getJSON = function(url) {

            var xhr = new XMLHttpRequest();
            xhr.open('GET', url, true);
            xhr.responseType = 'json';

            //(xhr.onload removed here because it was used for returning a callback, which I don't need.)

            xhr.send();
        }
        console.log(getJSON('url'))

And of course it returns undefined.

So I don't know, I'm not a coder, so I'm confused out of my head here. I'm sorry if this is really basic and i'm just being retarded.

I'm going to assume that this is riot games API from the json you provided (CH0G4TH, SummonerID).

-

There are quite a lot of library's/modules that you can use for riotsAPI but I would suggest: LeagueJS (a lot of other ones are garbage). since you said that you are not a coder I would suggest you use this rather then figuring it out yourself, makes it a lot easier.

Note

You will need a riot-api key which you can get from this link:

https://developer.riotgames.com/

Example Code:

 const LeagueJS = require('leaguejs'); // Here you should put your RGAPI code const leagueJs = new LeagueJS("RGAPI-SylasReallyBeBrokenRn"); // I like to create a region variable (makes it easier to change later on) const region = "oce" // although you dont need to have a user variable I will be using one for example purposes const user = "AIIChat" // Here I can get entries by name. It returns a body which I will log leagueJs.Summoner.gettingByName(user, region).then(user => { console.log(user) }) // Returns {......}

Endpoints:

( they dont really have docs)

  • ChampionEndPoint

     - gettingRotations
  • ChampionMasteryEndpoint

     - gettingBySummoner - gettingScoresBySummoner
  • LeagueEndpoint

     - gettingChallengerLeague - gettingMasterLeague - gettingGrandMasterLeague - gettingLeagueById - gettingEntriesForSummonerId - gettingLeagueEntriesForSummonerId - gettingEntries
  • LolStatusEndpoint

     - gettingShardData
  • MatchEndpoint

     - gettingById - gettingTimelineById - gettingListByAccount - gettingListByAccountWithoutPagination - gettingRecentListByAccount - gettingIdsByTournament - gettingByIdForTournament
  • SpectatorEndpoint

     - gettingActiveGame - gettingFeaturedGames
  • SummonerEndpoint

     - gettingByName - gettingByAccount - gettingById - gettingByPUUID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM