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}
0 commit comments