Subject: | Allow the default host be superseded by absolute URLs |
Hi. I'm the author of JIRA::REST and a few other modules that depend on REST::Client. Thank you for your time saver module!
My modules usually create a REST::Client object passing to it a default host to make it easier to interact with the server using relative URLs. However, sometimes the result of a REST method contains absolute URLs referencing related resources and it would be useful to be able to use them as is in subsequent requests.
As a concrete example, I'm using JIRA's REST API to grok a project's roles using this method: https://docs.atlassian.com/jira/REST/server/#api/2/project/{projectIdOrKey}/role-getProjectRoles.
JIRA::REST's API allows the user to pass only the "/rest/api/2/project/{projectIdOrKey}/role" method path. However, the method's result contain absolute URLs like this:
Show quoted text
> {
> "Administrators": "http://www.example.com/jira/rest/api/2/project/MKY/role/10002",
> "Users": "http://www.example.com/jira/rest/api/2/project/MKY/role/10001",
> "Developers": "http://www.example.com/jira/rest/api/2/project/MKY/role/10000"
> }
Currently I have to remove the "http://www.example.com/jira" prefix from them before passing them to REST::Client::GET because the method REST::Client::_prepareURL always prefix the URL with the default host, even if it gets an absolute URL.
It would be very convenient if it could leave absolute URLs intact, using the default host only to prefix relative URLs.
What do you think?
I'm attaching a patch that can be applied to v273 to implement this.
BTW, do you have a public repository for REST::Client?
Thanks!
Subject: | rest-client.patch |
diff --git a/lib/REST/Client.pm b/lib/REST/Client.pm
index fa35fec..6e77723 100644
--- a/lib/REST/Client.pm
+++ b/lib/REST/Client.pm
@@ -102,8 +102,8 @@ The config flags are:
=item host
-A default host that will be prepended to all requests. Allows you to just
-specify the path when making requests.
+A default host that will be prepended to all requests using relative URLs.
+Allows you to just specify the path when making requests.
The default is undef - you must include the host in your requests.
@@ -476,6 +476,9 @@ sub _prepareURL {
my $self = shift;
my $url = shift;
+ # Do not prepend default host to absolute URLs.
+ return $url if $url =~ /^https?:/;
+
my $host = $self->getHost;
if($host){
$url = '/'.$url unless $url =~ /^\//;