3

In my app I want to fetch Live Price by Flight Details for this I have used SkyScanner API. I have read the documentation before obtained data I have to create 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. So How can I get Sessionkey as I understood it must be provided by API Server.

Here is my try:

require 'json'
require 'net/http'
require 'uri'


  post_params = { 
    :apiKey => "[API_KEY]",
    :country => "GB",
    :currency => "GBP",
    :locale => "en-GB",
    :adults =>1,
    :children => 0,
    :infants => 0,
    :originplace => '11235',
    :destinationplace => '13554',
    :outbounddate => '2015-05-19',
    :inbounddate => '2015-05-26',
    :locationschema => 'Default',
    :cabinclass => 'Economy',
    :groupPricing => true
  }


sessionkey_request = Net::HTTP.post_form(URI.parse('http://partners.api.skyscanner.net/apiservices/pricing/v1.0'), post_params )
get_data= "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=[API_KEY]"
puts sessionkey_request.inspect
temp = Net::HTTP.get_response(URI.parse(get_data)).body
# puts temp

In console I get

<Net::HTTPCreated 201 Created readbody=true> # sessionkey_request.inspect

Not getting SessionKey in response without it I can not retrieved data. Please guide me where I do mistake. I appreciate for solution.

Fore more details and live result Check Demo by API

Note: I have check gem 'skyscanner ' but it doesn't provide any method for Live Price. It provide Browse Cache methods.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
Hetal Khunti
  • 787
  • 1
  • 9
  • 23
  • I replaced your actual API Key with a placeholder as it's probably not a good idea for it to be public. – martincarlin87 May 12 '15 at 10:52
  • @martincarlin87 : I am aware about secret key should not posted but it's not my real APi-key. I have taken it from demo site only. It's public. Anyways Thanks :) – Hetal Khunti May 12 '15 at 10:54
  • ah, ok. I'm not familiar with the Skyscanner API but if you are getting a 201 status then you can't be far away from getting the session key but from reading the docs it says `A successful response contains no content. The URL to poll the booking details is specified in the Location header of the response.`. Have you tried something like `puts temp["location"]` or whatever the response object is? – martincarlin87 May 12 '15 at 11:01
  • as I have declared I am getting `{}` as `temp` object. I have inspect on it – Hetal Khunti May 12 '15 at 11:21
  • yes but as the docs say, the body is empty when you have a successful request, the session key you need is in the form of the url for future requests but it's in the header and not the body. – martincarlin87 May 12 '15 at 11:27
  • I know requested successful but without knowing `sessionKey` how can I retrieved data. Somebody has post the solution let me try. – Hetal Khunti May 12 '15 at 11:31
  • @HetalKhunti : as martincarlin87 informed you, you were very near with the solution. I have post the answer please check it – Gagan Gami May 12 '15 at 11:41

1 Answers1

4

As per the doc:

A successful response contains no content. The URL to poll the booking details is specified in the Location header of the response

so try this:

sessionkey_request["location"]

I have test it on my system and it returns me:

http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D

last part is your sessionKey which you can use for GET request. If you want only last part(sessionKey) then you can retrieved it by:

 > url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
 > url.split('/').last
 => "8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D" 
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
  • Oh.. man you save my day. I was sooo near with this but didn't try this way. Thanks now it works like charm. Thanks a lots – Hetal Khunti May 12 '15 at 11:42
  • @GaganGami I'm also facing the same issue with status code 415. For testing purpose. i requested through POSTMAN. Can you guide me to fix the issue? – iSrinivasan27 Jul 29 '16 at 07:26
  • @i-Droid: sure can you post question on SO, with what have you done/ tried so far, and also don't forget to post your code – Gagan Gami Jul 29 '16 at 07:43
  • @GaganGami Sure but i didn't start code yet. What i did was Created an account and tried to create a session using POSTMAN. Is this right or i must request through my applicaion?? – iSrinivasan27 Jul 29 '16 at 07:48