I can not use the .json of an api

1

Someone knows why I'm doing wrong, because I want to get to work with this riot api and use the .json that I see when I access the link. the only thing that returns when I use this code is a "response" and without the .json.

PDT: in the link, after the "api-ket=" is a key that expires, I will renew it when alguein asks me for help

below the code:

fetch('https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/Einstein?api_key=RGAPI-998c0637-97db-4460-a57f-55db7b9c2f36', {
        method: "GET",          
        mode: "no-cors",
        redirect:"follow",
        Headers : new Headers({
            'Content-Type': 'text/plain',
        cache: 'default',
        }),
    }) 

  .then((response) => {     
        console.log(response)       
  })
    
asked by Jóse M Montaño 07.02.2018 в 02:17
source

1 answer

0

fetch(...) is a Promise that returns an object response . Inside the response object you have the json() method that in turn, returns a Promise .

fetch('https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/Einstein?api_key=RGAPI-998c0637-97db-4460-a57f-55db7b9c2f36', {
      method: "GET",          
      mode: "no-cors",
      redirect:"follow",
      Headers : {
          'Content-Type': 'application/json',
      cache: 'default',
      },
  })
  .then((response) => {
    response.json().then((data) => {
      console.dir(data)
    })       
  })

Async / Await

If you are working with support async/await , you can write this way

const getData = async () => {
  const response = await fetch('https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/Einstein?api_key=RGAPI-998c0637-97db-4460-a57f-55db7b9c2f36', {
    method: "GET",          
    mode: "no-cors",
    redirect:"follow",
    Headers : {
      'Content-Type': 'application/json',
      cache: 'default',
    }})
  return await response.json()
}

(async () => {
  const data = await getData()
  console.dir(data)
})()
    
answered by 13.02.2018 в 22:25