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.

UrlEncoding does not handle DateTimes correctly #8150

Closed
@danbarua

Description

@danbarua

Possibly related to #6128

I'm having similar issues to the person in this StackOverflow question: http://stackoverflow.com/a/17355012/128228
It seems people are working around the issue by using Jquery's $.param function but it would be nice to get this issue fixed in Angular.

I'm making a request using $http.GET with params that look like this:

{
  startDateTime: "2014-06-30T23:00:00.000Z",
  endDateTime: "2014-07-07T23:00:00.000Z"
}

and the resulting HTTP request seems to be wrapping the datetime in quotes and then escaping the quotes resulting in this query string:

?endDateTime=%222014-07-07T23:00:00.000Z%22&startDateTime=%222014-06-30T23:00:00.000Z%22

The offending line seems to be https://github.com/angular/angular.js/blob/master/src/ng/http.js#L993 which encodes a datetime into the string ""2014-06-30T23:00:00.000Z"" (note the double quotes).

I've implemented the following hack which works for me, but perhaps there may be a better way...

function buildUrl(url, params) {
          if (!params) return url;
          var parts = [];
          forEachSorted(params, function(value, key) {
            if (value === null || isUndefined(value)) return;
            if (!isArray(value)) value = [value];

            forEach(value, function(v) {
              if (v instanceof Date){
                  v = v.toISOString(); //toISOString() only supported in IE8 and above
              }
              else if (isObject(v)) {
                v = toJson(v);
              }
              parts.push(encodeUriQuery(key) + '=' +
                         encodeUriQuery(v));
            });
          });
          if(parts.length > 0) {
            url += ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
          }
          return url;
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions