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

Commit 73c8593

Browse files
committed
feat(http): added params parameter
The params parameter can now be used to serialize parameters in the URLs. The serialization does proper escaping and JSON encoding if it is an object.
1 parent ac75079 commit 73c8593

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/service/http.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
'use strict';
23

34
/**
45
* Parse headers into key value object
@@ -360,6 +361,8 @@ function $HttpProvider() {
360361
*
361362
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
362363
* - **url** – `{string}` – Absolute or relative URL of the resource that is being requested.
364+
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
365+
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
363366
* - **data** – `{string|Object}` – Data to be sent as the request message data.
364367
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
365368
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
@@ -638,7 +641,8 @@ function $HttpProvider() {
638641
var deferred = $q.defer(),
639642
promise = deferred.promise,
640643
cache,
641-
cachedResp;
644+
cachedResp,
645+
url = buildUrl(config.url, config.params);
642646

643647
$http.pendingRequests.push(config);
644648
promise.then(removePendingReq, removePendingReq);
@@ -649,7 +653,7 @@ function $HttpProvider() {
649653
}
650654

651655
if (cache) {
652-
cachedResp = cache.get(config.url);
656+
cachedResp = cache.get(url);
653657
if (cachedResp) {
654658
if (cachedResp.then) {
655659
// cached request has already been sent, but there is no response yet
@@ -665,13 +669,13 @@ function $HttpProvider() {
665669
}
666670
} else {
667671
// put the promise for the non-transformed response into cache as a placeholder
668-
cache.put(config.url, promise);
672+
cache.put(url, promise);
669673
}
670674
}
671675

672676
// if we won't have the response in cache, send the request to the backend
673677
if (!cachedResp) {
674-
$httpBackend(config.method, config.url, reqData, done, reqHeaders, config.timeout);
678+
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout);
675679
}
676680

677681
return promise;
@@ -686,10 +690,10 @@ function $HttpProvider() {
686690
function done(status, response, headersString) {
687691
if (cache) {
688692
if (isSuccess(status)) {
689-
cache.put(config.url, [status, response, parseHeaders(headersString)]);
693+
cache.put(url, [status, response, parseHeaders(headersString)]);
690694
} else {
691695
// remove promise from the cache
692-
cache.remove(config.url);
696+
cache.remove(url);
693697
}
694698
}
695699

@@ -719,5 +723,21 @@ function $HttpProvider() {
719723
if (idx !== -1) $http.pendingRequests.splice(idx, 1);
720724
}
721725
}
726+
727+
728+
function buildUrl(url, params) {
729+
if (!params) return url;
730+
var parts = [];
731+
forEachSorted(params, function(value, key) {
732+
if (value == null || value == undefined) return;
733+
if (isObject(value)) {
734+
value = toJson(value);
735+
}
736+
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
737+
});
738+
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
739+
}
740+
741+
722742
}];
723743
}

test/service/httpSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ describe('$http', function() {
132132
// TODO(vojta): test passing timeout
133133

134134

135+
describe('params', function() {
136+
it('should do basic request with params and encode', inject(function($httpBackend, $http) {
137+
$httpBackend.expect('GET', '/url?a%3D=%3F%26&b=2').respond('');
138+
$http({url: '/url', params: {'a=':'?&', b:2}, method: 'GET'});
139+
}));
140+
141+
142+
it('should merge params if url contains some already', inject(function($httpBackend, $http) {
143+
$httpBackend.expect('GET', '/url?c=3&a=1&b=2').respond('');
144+
$http({url: '/url?c=3', params: {a:1, b:2}, method: 'GET'});
145+
}));
146+
147+
148+
it('should jsonify objects in params map', inject(function($httpBackend, $http) {
149+
$httpBackend.expect('GET', '/url?a=1&b=%7B%22c%22%3A3%7D').respond('');
150+
$http({url: '/url', params: {a:1, b:{c:3}}, method: 'GET'});
151+
}));
152+
});
153+
154+
135155
describe('callbacks', function() {
136156

137157
it('should pass in the response object when a request is successful', function() {

0 commit comments

Comments
 (0)