0

I am working with SkyScanner API to fetch Live Prices by Flight Details.

As the documentation says. I created Live Pricing Service Session. Which can be created by post request to api and then it provide SessionKey by using this SessionKey and apiKey I can retrived the data. I can see the the sessionKey with getResponseHeader("Location") that I use in the success method. And I submit it to a global variable urlSession, that I use later in another http get request as a url. I can see the SessionKey in the alert but I have a undefined error when I try to use it in the get method. I am not sure if it's a CORS problem, or just a syntax.

var hrpost = Ti.Network.createHTTPClient();
// post_params
  var post_params = {
      "apiKey" : {apiKey},
       "Country" : "US",
    "Currency" : "USD",
    "Locale" : "en-GB",
    "Adults" : 1,
    "Children" : 0,
    "Infants" : 0,
    "OriginPlace" : "16216",
    "DestinationPlace" : "1111",
    "OutboundDate" : "2016-09-23",
    "InboundDate" : "2016-09-30",
    "LocationSchema" : "Default",
    "CabinClass" : "Economy",
    "GroupPricing" : true

  };
  var strMyObj = JSON.stringify(post_params);
  //Here I set the webservice address and method
hrpost.open('POST', "http://partners.api.skyscanner.net/apiservices/pricing/v1.0");
 //Set the headers
  hrpost.setRequestHeader("Accept", "application/json");
hrpost.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
hrpost.send(post_params);

hrpost.onload = function(){

    alert('Posted successfully');
    urlsessionKey = hrpost.getResponseHeader('Location');
    alert(urlsessionKey);

}
 hrpost.onerror = function() {
alert('request didnt posted');
var rejection = {
            status: hrpost.status,
            statusText: hrpost.statusText,


        };
        alert(rejection);
};



//HTTP request get method to send the sessionKey and retrieve data
var xmlHttp = Ti.Network.createHTTPClient();



 var post_params = {
      "apiKey" : {apiKey},
       "Country" : "US",
    "Currency" : "USD",
    "Locale" : "en-GB",
    "Adults" : 1,
    "Children" : 0,
    "Infants" : 0,
    "OriginPlace" : "16216",
    "DestinationPlace" : "1111",
    "OutboundDate" : "2016-09-23",
    "InboundDate" : "2016-09-30",
    "LocationSchema" : "Default",
    "CabinClass" : "Economy",
    "GroupPricing" : true

  };
//here the error occurs when I use the sessionKey I stored in the global var urlsessionKey  
xmlHttp.open('GET', urlsessionKey);

   xmlHttp.setRequestHeader("Accept", "application/json");
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


xmlHttp.send(post_params);

xmlHttp.onload = function(e){

     alert('Get successfully');
}
 xmlHttp.onerror = function() {
 alert('request didnt posted');
 var rejection = {
            status: xmlHttp.status,
            statusText: xmlHttp.statusText,

        };
        alert(rejection);
};
Ayoub Salhi
  • 312
  • 1
  • 4
  • 19

1 Answers1

0

I think maybe the error is because you aren't using the api key to your call when polling session as it says in the documentation. Try it like this:

   var urlsessionKey = hrpost.getResponseHeader('Location') + "?apiKey=" + {apiKey};

Anyways in the FAQ of Skyscanner appears that Flights Live Pricing service doesn't allow CORS version, so maybe you have to do this call from your server side. I posted some days ago my example in Java of the first call if it's helpful for you.

Hope it helps!

Community
  • 1
  • 1
charly3pins
  • 389
  • 1
  • 11
  • Thanks for your answer. I was able to pull the sessionKey with hrpost.getResponseHeader('Location'), without appending the apiKey. The problem with my code was the order of calls I was doing, I set the post http request to false asynchronous. And I made my get request inside the onload method of the post request. I was able to solve the problem with this architecture. – Ayoub Salhi Aug 30 '16 at 18:44