Client URL x Request Path
bruno-brant opened this issue · 3 comments
More of a question than an issue.
When I create a JsonClient in node I do the following:
var client = restify.createJsonClient({
url: 'https://www.domain.com:4321/api'
});
Once I've done that, I make calls like so:
client.post('/service/path', { });
Which seems right. I expect that the path called would be something like https://www.domain.com:4321/api/service/path
. However, what is happening is that the client is throwing away the /api base path and calling https://www.domain.com:4321/service/path
.
I don't get it - I'm inserting the client URL into a config file, so that I can change hosts without any hassle; Now that I need a base path, I need to change the code as well as the config.
You are right, that is very confusing.
Looks like this is happening because of how options are passed into the various client methods. The client doesn't do the right thing and preserve any paths from the original url, rather it overrides it when you pass your request path into the http method based functions. See these lines for what's going on under the hood: https://github.com/restify/clients/blob/master/lib/HttpClient.js#L662-L664.
We should make this consistent and allow passing in of the prefix path or at least preserve the path from the original client creation.
In order to work around this for now you can do the following:
var client = restify.createJsonClient({
url: 'https://www.domain.com:4321/api'
});
client.post(client.url.path + '/service/path', { });
I've made a wrapper over the clients (for another reason - I wanted a promise API), so I fixed it there.
I think I might be able to do a PR to fix this behavior.
We just encountered this today. Moved some services behind a proxy, so every service got to the same host with different base paths.
We thought this would only need some evn variable changes, but it can't be solved without changing code. In our case, a lot of it.
This should really be fixed.