سؤال

I'm trying to use the Faraday gem (version 0.8.4) to interact with an external API. The API requires a trailing slash on the URL, e.g., https://api.example.com/1.2/. Here's the code I'm using to make the request:

connection = Faraday.new(:url => 'https://api.example.com/1.2/')
response = connection.get do |request|
  request.params['api_key'] = 'MY_KEY'
end

Upon inspection of the response, though, I see that the trailing slash was stripped from the URL:

response.env[:url]
=> #<URI::HTTPS:0x007fda3513d5f0 URL:https://api.wpengine.com/1.2?api_key=MY_KEY>

I'm having a hard time figuring out how to prevent the slash from being stripped. Does anyone know how to do that?

هل كانت مفيدة؟

المحلول

I was able to make it work by moving the trailing slash into the get method:

connection = Faraday.new(:url => 'https://api.example.com')
response = connection.get('/1.2/') do |request|
  request.params['api_key'] = 'MY_KEY'
end

response.env[:url]
=> #<URI::HTTPS:0x007fdb95166f98 URL:https://api.example.com/1.2/?api_key=MY_KEY>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top