如何防止Faraday从URL中删除尾部斜杠?(How do I prevent Faraday from stripping the trailing slash from a URL?)

我正在尝试使用法拉第宝石(版本0.8.4)与外部API进行交互。 API要求URL上有一个斜杠,例如https://api.example.com/1.2/ 。 这是我用来发出请求的代码:

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

但是,在检查响应后,我发现从URL中删除了尾部斜杠:

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

我很难弄清楚如何防止斜线被剥离。 有谁知道这是怎么做到的吗?

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?

最满意答案

我可以通过将尾部斜杠移动到get方法来使其工作:

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>

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>

更多推荐

URL,com,I'm,response,api,电脑培训,计算机培训,IT培训"/> <meta name="