Question

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.

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top