在单独的获取呼叫中使用解析响应



我是Ruby和API的新手,所以如果这非常简单,我很抱歉...我需要有脚本将首先发布以启动导出文件的创建,然后进行get呼叫以检索文件。Get通话需要使用JSON响应的一部分。

我正在使用httparty宝石。

我认为我需要创建一个等于解析的JSON的变量,然后使该变量的呼叫中的一个变量,但我不清楚如何做到这一点。帮助您表示赞赏。

require 'httparty'
url = 'https://api.somewhere.org'
response = HTTParty.post(url)
puts response.parse_response

JSON响应:

 export_files"=>
   {"id"=> #####,   
   "export_id"=> #####, 
   "status"=>"Queued"}}

在我的通话中,我需要使用URL中的export_id号码。

HTTParty.get('https://api.somewhere.org/export_id/####')

如评论中所述

require 'httparty'
require 'json'
url = 'https://api.somewhere.org'
response = HTTParty.post(url)
if hash = JSON.parse(response.body)
  if export_id = hash[:export_files][:export_id]
    post = HTTParty.post("https://api.somewhere.org/export_id/#{export_id}")
  end
else
  # handle error 
end

相关内容

最新更新