无法将 ruby 变量插入字符串 url 以调用 JSON API
Cannot interpolate ruby variable into string url to call JSON API
我正在尝试向 JSON api 发出 GET 请求,遍历多个 JSON 对象以提取它们的 ID,然后将它们输入到 url 发出请求。令牌部分工作正常,但我无法弄清楚我的每个迭代器都有问题。
此 api 请求的 GET url 示例是:
https://api.hailoapp.com/business/read?id=12345
api 应该 return 一个有效的响应,但我不断收到 api 文档所说的 400 错误,这意味着没有 ID。所以我的代码一定有问题:
require "json"
require "httparty"
# LOGIN
login_response = HTTParty.post("https://api.hailoapp.com/auth/login",
{
:body => { :user => "email@email.com", :password => "password" }.to_json,
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded" }
})
data = login_response.to_hash
api_token = data["api_token"]
# RETRIEVE ACCOUNT
restaurants = JSON.parse File.read('file.json')
input_id = restaurants.each do |r| r["id"]
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{input_id}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
puts retrieve_response.body
puts retrieve_response.code
puts retrieve_response.message
end
我在控制台试过这个:restaurants.each { |r| puts r["id"] }
但不知道如何让它与主代码一起工作来访问 api.
示例JSON数据:
{
"id": "137072",
"name": "The Brackenbury",
"phone": "+442087414928",
"email": "table@brackenburyrestaurant.co.uk",
"website": "http://brackenburyrestaurant.co.uk/",
"location": {
"latitude": 51.4978732,
"longitude": -0.2313129,
"address": {
"line1": "129-131 Brackenbury Road",
"line2": "Hammersmith",
"line3": "",
"postcode": "W6 0BQ",
"city": "London",
"country": "UK"
}
}
}
当我使用此代码向 api 发出类似的 POST 请求时,它工作正常。
此代码...
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{input_id}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
引用一个名为 input_id
的变量,但这实际上是整个迭代而不是每个实例。
你想要的是...
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{r[:id]}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
这将使用 r 的 :id
值检索每个迭代实例(实例称为 r
)的响应。
您不需要每个块顶部的独立 r[:id]
,请将其删除。
我正在尝试向 JSON api 发出 GET 请求,遍历多个 JSON 对象以提取它们的 ID,然后将它们输入到 url 发出请求。令牌部分工作正常,但我无法弄清楚我的每个迭代器都有问题。
此 api 请求的 GET url 示例是: https://api.hailoapp.com/business/read?id=12345
api 应该 return 一个有效的响应,但我不断收到 api 文档所说的 400 错误,这意味着没有 ID。所以我的代码一定有问题:
require "json"
require "httparty"
# LOGIN
login_response = HTTParty.post("https://api.hailoapp.com/auth/login",
{
:body => { :user => "email@email.com", :password => "password" }.to_json,
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded" }
})
data = login_response.to_hash
api_token = data["api_token"]
# RETRIEVE ACCOUNT
restaurants = JSON.parse File.read('file.json')
input_id = restaurants.each do |r| r["id"]
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{input_id}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
puts retrieve_response.body
puts retrieve_response.code
puts retrieve_response.message
end
我在控制台试过这个:restaurants.each { |r| puts r["id"] }
但不知道如何让它与主代码一起工作来访问 api.
示例JSON数据:
{
"id": "137072",
"name": "The Brackenbury",
"phone": "+442087414928",
"email": "table@brackenburyrestaurant.co.uk",
"website": "http://brackenburyrestaurant.co.uk/",
"location": {
"latitude": 51.4978732,
"longitude": -0.2313129,
"address": {
"line1": "129-131 Brackenbury Road",
"line2": "Hammersmith",
"line3": "",
"postcode": "W6 0BQ",
"city": "London",
"country": "UK"
}
}
}
当我使用此代码向 api 发出类似的 POST 请求时,它工作正常。
此代码...
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{input_id}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
引用一个名为 input_id
的变量,但这实际上是整个迭代而不是每个实例。
你想要的是...
retrieve_response = HTTParty.get("https://api.hailoapp.com/business/read?id=#{r[:id]}",
{
:headers => { "Content-Type" => "text", "Accept" => "application/x-www-form-urlencoded", "Authorization" => "token #{api_token}" }
})
这将使用 r 的 :id
值检索每个迭代实例(实例称为 r
)的响应。
您不需要每个块顶部的独立 r[:id]
,请将其删除。