简体   繁体   中英

How do I prevent Faraday from stripping the trailing slash from a URL?

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, eg, 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM