Alexander Popov
Alexander Popov

Reputation: 24905

How to initialize Faraday with a url of type "http://example.com/something"?

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.

Upvotes: 1

Views: 1077

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

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)

Upvotes: 5

Related Questions