I'm trying to make a GET request to a JSON api, iterating over a number of JSON objects to extract their ids and then input them into the url to make the request. The token part works fine, but there is something wrong with my each iterator which I can't figure out.
An example GET url for this api request is: https://api.hailoapp.com/business/read?id=12345
The api should return a valid response but I keep getting a 400 error which the api doc says, means there is no id. So there must be something wrong with my code:
require "json"
require "httparty"
# LOGIN
login_response = HTTParty.post("https://api.hailoapp.com/auth/login",
{
:body => { :user => "[email protected]", :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
I tried this in the console: restaurants.each { |r| puts r["id"] } but don't know how to make it work together with the main code to access the api.
Example JSON data:
{
"id": "137072",
"name": "The Brackenbury",
"phone": "+442087414928",
"email": "[email protected]",
"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"
}
}
}
When I did a similar POST request to the api using this code, it worked fine.
input_id = restaurants.each do |r| r["id"]is correct?