Skip to content
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 opened this issue Jul 11, 2014 · 1 comment
Closed

UrlEncoding does not handle DateTimes correctly #8150

danbarua opened this issue Jul 11, 2014 · 1 comment

Comments

@danbarua
Copy link
Contributor

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;
        }
@danbarua
Copy link
Contributor Author

Alternative approach as discussed in #6128

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 (isObject(v)) {
                if (isDefined(v.toJSON)){
                  v = v.toJSON(); 
                }
                else {
                  v = toJson(v);
                }
              }
              parts.push(encodeUriQuery(key) + '=' +
                         encodeUriQuery(v));
            });
          });
          if(parts.length > 0) {
            url += ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
          }
          return url;
        }

danbarua added a commit to danbarua/angular.js that referenced this issue Jul 11, 2014
$http was wrapping dates in double quotes leading to query strings like this:
  ?date=%222014-07-07T23:00:00.000Z%22
Instead of calling JSON.stringify, this fix checks to see if a param object
has a toJSON() function and uses that for encoding, falling back to
JSON.stringify as per the previous behaviour.

Closes angular#8150 and angular#6128
danbarua added a commit to danbarua/angular.js that referenced this issue Jul 11, 2014
$http was wrapping dates in double quotes leading to query strings like this:
  ?date=%222014-07-07T23:00:00.000Z%22
Instead of calling JSON.stringify, this fix checks to see if a param object
has a toJSON() function and uses that for encoding, falling back to
JSON.stringify as per the previous behaviour.

Closes angular#8150 and angular#6128
@IgorMinar IgorMinar added this to the 1.3.0-beta.16 milestone Jul 15, 2014
IgorMinar pushed a commit that referenced this issue Jul 16, 2014
This commit special cases date handling rather than calling toJSON as we always need
a string representation of the object.

$http was wrapping dates in double quotes leading to query strings like this:
  ?date=%222014-07-07T23:00:00.000Z%22

Closes #8150
Closes #6128
Closes #8154
ckknight pushed a commit to ckknight/angular.js that referenced this issue Jul 16, 2014
This commit special cases date handling rather than calling toJSON as we always need
a string representation of the object.

$http was wrapping dates in double quotes leading to query strings like this:
  ?date=%222014-07-07T23:00:00.000Z%22

Closes angular#8150
Closes angular#6128
Closes angular#8154
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants