0

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.

1
  • are you sure the way you are looping around input_id = restaurants.each do |r| r["id"] is correct? Commented Jul 24, 2015 at 12:07

1 Answer 1

1

This code ...

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}" }
  }) 

References a variable called input_idbut that's actually the whole iteration not each instance.

What you want is...

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}" }
  }) 

Which will retrieve the response for each instance of the iteration (the instance is called r) using r's :id value.

You don't need the standalone r[:id] at the top of the each block, remove that.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @SteveTurczn, so I just do this at the top of the block: restaurants.each do |r| and then interpolate with: #{r[:id] and then end at the bottom?
That works great @SteveTurczyn thanks v. much, you are the man! haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.