If I do:

conn = Faraday.new(url: 'http://example.com/api') do |builder|
  builder.adapter :httpclint
end

it removes /api from the base url and conn.host returns "example.com". When I later do:

conn.post { |req| req.url '/resource'...}

it calls example.com/resource, instead of example.com/api/resource. How can I change that so it doesn't cut the base url?

I know I could initialize it with only example.com and then just do something like:

conn.post { |req| req.url '/api/resource'...}

but I want to store the base url in a global configuration, so that only the names of the resources are used in the code.

有帮助吗?

解决方案

Omit the forward slash at the beginning of the request and it'll be relative to the initial URL you supplied. Otherwise, it will start from the root of the host:

conn.post { |req| req.url 'resource'...}  # Treated as example.com/api/resource
conn.post { |req| req.url '/resource'...} # Treated as example.com/resource

This is by design (See this GitHub issue)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top