@@ -13,7 +13,8 @@ var http = require("http"),
13
13
specs = require ( "./specs" ) ,
14
14
helper = require ( "./helper" ) ,
15
15
server = require ( "./server" ) ,
16
- mapping = require ( "./mapping" ) ;
16
+ mapping = require ( "./mapping" ) ,
17
+ qs = require ( 'querystring' ) ;
17
18
18
19
var reSpace = / \s / ,
19
20
reConnectivity =
@@ -57,8 +58,8 @@ var filenames = {
57
58
cached : "_Cached" ,
58
59
} ;
59
60
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 ) {
62
63
var protocol , options ;
63
64
64
65
if ( proxy ) {
@@ -79,6 +80,7 @@ function get(config, pathname, proxy, agent, callback, encoding) {
79
80
headers : {
80
81
Host : config . hostname ,
81
82
} ,
83
+ method : config . method
82
84
} ;
83
85
} else {
84
86
protocol = config . protocol === "https:" ? https : http ;
@@ -87,10 +89,15 @@ function get(config, pathname, proxy, agent, callback, encoding) {
87
89
host : config . hostname ,
88
90
auth : config . auth ,
89
91
port : config . port ,
92
+ method : config . method ,
90
93
headers : { } ,
91
94
} ;
92
95
}
93
96
97
+ if ( options . method == "POST" ) {
98
+ options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
99
+ }
100
+
94
101
if ( encoding !== "binary" ) {
95
102
options . headers [ "X-WPT-API-KEY" ] = this . config . key ;
96
103
options . headers [ "accept-encoding" ] = "gzip,deflate" ;
@@ -101,8 +108,8 @@ function get(config, pathname, proxy, agent, callback, encoding) {
101
108
options . agent = agent ;
102
109
}
103
110
104
- return protocol
105
- . get ( options , function getResponse ( res ) {
111
+ var request = protocol
112
+ . request ( options , function getResponse ( res ) {
106
113
var data ,
107
114
length ,
108
115
statusCode = res . statusCode ;
@@ -159,6 +166,13 @@ function get(config, pathname, proxy, agent, callback, encoding) {
159
166
. on ( "error" , function onError ( err ) {
160
167
callback ( err ) ;
161
168
} ) ;
169
+
170
+ if ( options . method == "POST" ) {
171
+ return request . end ( qs . stringify ( data ) ) ;
172
+ } else {
173
+ return request . end ( ) ;
174
+ }
175
+
162
176
}
163
177
164
178
// execute callback properly normalizing optional args
@@ -186,22 +200,33 @@ function api(pathname, callback, query, options) {
186
200
config = this . config ;
187
201
}
188
202
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 ,
191
207
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
+ }
193
217
194
218
if ( options . dryRun ) {
195
219
// dry run: return the API url (string) only
196
220
if ( typeof callback === "function" ) {
197
- callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname ) ] ) ;
221
+ callback . apply ( callback , [ undefined , helper . dryRun ( config , pathname , query ) ] ) ;
198
222
}
199
223
} else {
200
224
// make the real API call
201
225
get . call (
202
226
this ,
203
227
config ,
204
228
pathname ,
229
+ query ,
205
230
options . proxy ,
206
231
options . agent ,
207
232
function apiCallback ( err , data , info ) {
0 commit comments