Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$http no longer accepts "string-like" URLs #14557

Closed
@schmod

Description

@schmod

Since Angular 1.4.9, the $http service only accepts pure strings as URLs. If I pass in an object that has a toString() method, $http raises an exception:

var url = { 
   toString: function() { 
        return 'http://www.example.com';
   }
};

$http.get(url); // Throws "Error: [$http:badreq] Http request configuration url must be a string."

This is a departure from Angular's pre-1.4.9 behavior, jQuery, and XMLHttpRequest; all of which accept (and correctly interpret) the url object described above. This behavior was introduced in 6628b4f to fix #12925, and merged in #13444.

I would recommend relaxing this restriction slightly, and allow objects with a toString() method to be used as URLs.


The relevant portion of $http's source currently looks like:

if (!isString(requestConfig.url)) {
      throw minErr('$http')('badreq', 'Http request configuration url must be a string.  Received: {0}', requestConfig.url);
}

I would recommend changing it to something like this:

var isStringlike = function(val) {
    return isString(val) || (isObject(val) && isFunction(val.toString));
};
if (!isStringlike(requestConfig.url)) {
      throw minErr('$http')('badreq', 'Http request configuration url must be a string.  Received: {0}', requestConfig.url);
}

While this would result in a less-satisfactory fix for #12925 (we might throw a less descriptive error in some cases), the behavior would also be more correct.

If this looks good, I can write up a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions