JoseTurron
JoseTurron

Reputation: 243

API does not collect required data in JavaScript

I'm trying to get route prices with Skyscanner API in JavaScript. I paste the snippet from Rapid API and put it in a function:

function takeData() {
    console.log("to jest test")
    fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
            "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00"
        }
    })
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err);
    });
}

takeData()

Instead of getting details as shown in Rapid API

enter image description here

I get this:

Response {type: "cors", url: "https://skyscanner-skyscanner-flight-search-v1.p.r…/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23"
__proto__: Response

Any ideas what may be more to do here? Am I missing something?

Upvotes: 0

Views: 65

Answers (2)

Paul Fitzgerald
Paul Fitzgerald

Reputation: 12129

You are close. You just need to update your fetch call to parse the response so that it returns json. See the second then block.

return response.json();

Full code below:

fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", {
    "method": "GET",
    "headers": {
      "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
      "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00"
    }
  })
  .then(response => {
    return response.json();
  })
  .then(json => {
    console.log(json);
  })
  .catch(err => {
    console.log(err);
});

Upvotes: 1

Marik Ishtar
Marik Ishtar

Reputation: 3049

you need to parse the response body as JSON :

try this

function takeData() {
    console.log("to jest test")
    fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
            "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00"
        }
    })
    .then(response => response.json())
    .then(res => console.log(res))
    .catch(err => {
        console.log(err);
    });
}

takeData()

Upvotes: 1

Related Questions