@@ -13,7 +13,8 @@ var http = require("http"),
1313 specs = require ( "./specs" ) ,
1414 helper = require ( "./helper" ) ,
1515 server = require ( "./server" ) ,
16- mapping = require ( "./mapping" ) ;
16+ mapping = require ( "./mapping" ) ,
17+ qs = require ( 'querystring' ) ;
1718
1819var reSpace = / \s / ,
1920 reConnectivity =
@@ -57,8 +58,8 @@ var filenames = {
5758 cached : "_Cached" ,
5859} ;
5960
60- // GET helper function
61- function get ( config , pathname , proxy , agent , callback , encoding ) {
61+ // GET/POST helper function
62+ function get ( config , pathname , data , proxy , agent , callback , encoding ) {
6263 var protocol , options ;
6364
6465 if ( proxy ) {
@@ -79,6 +80,7 @@ function get(config, pathname, proxy, agent, callback, encoding) {
7980 headers : {
8081 Host : config . hostname ,
8182 } ,
83+ method : config . method
8284 } ;
8385 } else {
8486 protocol = config . protocol === "https:" ? https : http ;
@@ -87,10 +89,15 @@ function get(config, pathname, proxy, agent, callback, encoding) {
8789 host : config . hostname ,
8890 auth : config . auth ,
8991 port : config . port ,
92+ method : config . method ,
9093 headers : { } ,
9194 } ;
9295 }
9396
97+ if ( options . method == "POST" ) {
98+ options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
99+ }
100+
94101 if ( encoding !== "binary" ) {
95102 options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
96103 options . headers [ "accept-encoding" ] = "gzip,deflate" ;
@@ -101,8 +108,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
101108 options . agent = agent ;
102109 }
103110
104- return protocol
105- . get ( options , function getResponse ( res ) {
111+ var request = protocol
112+ . request ( options , function getResponse ( res ) {
106113 var data ,
107114 length ,
108115 statusCode = res . statusCode ;
@@ -159,6 +166,13 @@ function get(config, pathname, proxy, agent, callback, encoding) {
159166 . on ( "error" , function onError ( err ) {
160167 callback ( err ) ;
161168 } ) ;
169+
170+ if ( options . method == "POST" ) {
171+ return request . end ( qs . stringify ( data ) ) ;
172+ } else {
173+ return request . end ( ) ;
174+ }
175+
162176}
163177
164178// execute callback properly normalizing optional args
@@ -186,22 +200,33 @@ function api(pathname, callback, query, options) {
186200 config = this . config ;
187201 }
188202
189- pathname = url . format ( {
190- pathname : url . resolve ( config . pathname , pathname ) ,
203+ pathname = url . resolve ( config . pathname , pathname ) ;
204+
205+ config . method = url . format ( {
206+ pathname : pathname ,
191207 query : query ,
192- } ) ;
208+ } ) . toString ( ) . length > 6 * 1024 ? "POST" : "GET" ;
209+
210+ if ( config . method == "GET" ) {
211+ pathname = url . format ( {
212+ pathname : pathname ,
213+ query : query ,
214+ } ) ;
215+ query = undefined ;
216+ }
193217
194218 if ( options . dryRun ) {
195219 // dry run: return the API url (string) only
196220 if ( typeof callback === "function" ) {
197- callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname ) ] ) ;
221+ callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
198222 }
199223 } else {
200224 // make the real API call
201225 get . call (
202226 this ,
203227 config ,
204228 pathname ,
229+ query ,
205230 options . proxy ,
206231 options . agent ,
207232 function apiCallback ( err , data , info ) {
0 commit comments