2

I am having difficulty calling an external API from my Ruby on Rails Controller

The following code is my Destinations Controller

url = URI.parse("https://api.sygictravelapi.com/1.0/en/places/list?parents=#{@destination.destination_type.downcase}:#{@destination.sygia_id}&level=poi")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true


req = Net::HTTP::Get.new(url.to_s)
req.add_field 'x-api-key', ENV["SYGIC_API_KEY"]

res = Net::HTTP.start(url.host, url.port) {
   |http|
   http.request(req)
}

The problem is that when I make this request, the response is "Bad Request", without any further information. Using the exact same request headers and URL through Postman, I get a 200-OK response from the endpoint, plus the data I expect.

When monitoring traffic using Wireshark, I see an HTTP request going to the API endpoint. But when using Postman, I see only TLS requests.

I've also tried different encoding of the Query String parameters to no avail. My assumption is that it is one of these two things that is causing the issue: 1) SSL not being used 2) QueryString Parameters not attached to the HTTP request. But would like additional eyes on my code to check I'm not missing something obvious.

Anthony Wood
  • 395
  • 1
  • 3
  • 16
  • 1
    I am not sure what could be the reason for this issue but if it is working in postman the easiest solution is copy code from the postman and paste it into your rails app. ``How to copy the code from Postman?`` Just follow the `code` option near the send button and select Ruby from the drop-down list. – Abdullah Nov 19 '17 at 15:26
  • @MuhammadAbdullah That is an incredibly useful feature of Postman, thanks for bring it to light! I've updated my answer below to show the code that would work. Do you have any thoughts on whether it is better or worse to use the Rest-Client Ruby Gem? – Anthony Wood Nov 19 '17 at 15:43
  • If it is working without adding any additional gem that there is no need to add ```RestClient ```. – Abdullah Nov 19 '17 at 15:52

1 Answers1

1

Rest-Client Ruby Gem: Help creating get request with Headers, Query, and setting a proxy

The problem turned out not to be with me but the search engine I was using. Because I live in China, I have to resort to Bing (for performance reasons)... But connecting to VPN, and with 1 Google Search I am able to find the link above.

The code below will solve the problem

body = RestClient.get "https://api.sygictravelapi.com/1.0/en/places/list?parents=#{@destination.destination_type.downcase}:#{@destination.sygia_id}&level=poi",
      headers = {accept: :json,  'x-api-key' => ENV["SYGIC_API_KEY"]}

If you don't want to install a new gem, the correct code from above is based on Postman's useful code generation feature:

require 'uri'
require 'net/http'

url = URI("https://api.sygictravelapi.com/1.0/en/places/list?parents=city%3A372&level=poi&limit=100")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)
request["x-api-key"] = ENV["SYGIC_API_KEY"]
request["cache-control"] = 'no-cache'

response = http.request(request)
puts response.read_body
Anthony Wood
  • 395
  • 1
  • 3
  • 16