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

fix($http): support url format variant for arrays value #fixes 7363 #7409

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ function $HttpProvider() {
},

xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN'
xsrfHeaderName: 'X-XSRF-TOKEN',

useArrayParams: false
};

/**
Expand Down Expand Up @@ -485,6 +487,7 @@ function $HttpProvider() {
* for more information.
* - **responseType** - `{string}` - see
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
* - **useArrayParams** - `{boolean}` - true if arrays should be sent as `param[]=val1&param[]=val2` instead of `param=val1&param=val2`.
*
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
* standard `then` method and two http specific methods: `success` and `error`. The `then`
Expand Down Expand Up @@ -594,7 +597,8 @@ function $HttpProvider() {
var config = {
method: 'get',
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse
transformResponse: defaults.transformResponse,
useArrayParams: defaults.useArrayParams
};
var headers = mergeHeaders(requestConfig);

Expand Down Expand Up @@ -852,7 +856,7 @@ function $HttpProvider() {
promise = deferred.promise,
cache,
cachedResp,
url = buildUrl(config.url, config.params);
url = buildUrl(config.url, config.params, config.useArrayParams);

$http.pendingRequests.push(config);
promise.then(removePendingReq, removePendingReq);
Expand Down Expand Up @@ -939,18 +943,19 @@ function $HttpProvider() {
}


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

forEach(value, function(v) {
if (isObject(v)) {
v = toJson(v);
}
parts.push(encodeUriQuery(key) + '=' +
parts.push(encodeUriQuery(key) + (valueIsArray && useArrayParams ? '[]' : '') + '=' +
encodeUriQuery(v));
});
});
Expand Down