![](/img/trans.png)
[英]Ruby On Rails : How to retrieve data from database on ajax request
[英]How to retrieve Session key from Skyscanner API post request - Ruby
在我的應用程序中,我想通過航班詳細信息獲取實時價格,因為我使用了SkyScanner API 。 我在獲取數據之前已經閱讀了文檔,我必須創建實時定價服務會話。 可以通過對api的post請求創建,然后通過使用此SessionKey
和apiKey
提供SessionKey
我可以重新獲取數據。 那么如何獲得Sessionkey,因為我知道它必須由API服務器提供。
這是我的嘗試:
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
在控制台我得到
<Net::HTTPCreated 201 Created readbody=true> # sessionkey_request.inspect
沒有它就沒有得到SessionKey我無法檢索數據。 請指導我錯誤的地方。 我很感激解決方案。
更多細節和實時結果通過API 檢查演示
注意:我已經檢查了gem'skyscanner'但它沒有為Live Price提供任何方法。 它提供Browse Cache方法。
根據文件:
成功的回復不包含任何內容。 輪詢預訂詳細信息的URL在響應的Location標頭中指定
試試這個:
sessionkey_request["location"]
我在我的系統上測試了它並返回給我:
http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D
最后一部分是您可以用於GET
請求的sessionKey
。 如果您只想要最后一個部分(sessionKey),那么您可以通過以下方式檢索它:
> url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
> url.split('/').last
=> "8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.