@@ -124,7 +124,9 @@ function Request(method, url) {
124
124
this . _formData = null ;
125
125
this . method = method ;
126
126
this . url = url ;
127
- this . header = { } ;
127
+ this . header = {
128
+ 'User-Agent' : 'node-superagent/' + pkg . version
129
+ } ;
128
130
this . writable = true ;
129
131
this . _redirects = 0 ;
130
132
this . redirects ( 5 ) ;
@@ -271,7 +273,7 @@ Request.prototype.set = function(field, val){
271
273
}
272
274
273
275
debug ( 'set %s "%s"' , field , val ) ;
274
- this . request ( ) . setHeader ( field , val ) ;
276
+ this . header [ field ] = val ;
275
277
return this ;
276
278
} ;
277
279
@@ -291,7 +293,8 @@ Request.prototype.set = function(field, val){
291
293
292
294
Request . prototype . unset = function ( field ) {
293
295
debug ( 'unset %s' , field ) ;
294
- this . request ( ) . removeHeader ( field ) ;
296
+
297
+ delete this . header [ field ] ;
295
298
return this ;
296
299
} ;
297
300
@@ -304,7 +307,7 @@ Request.prototype.unset = function(field){
304
307
*/
305
308
306
309
Request . prototype . get = function ( field ) {
307
- return this . request ( ) . getHeader ( field ) ;
310
+ return this . header [ field ] ;
308
311
} ;
309
312
310
313
/**
@@ -431,9 +434,7 @@ Request.prototype.query = function(val){
431
434
432
435
Request . prototype . send = function ( data ) {
433
436
var obj = isObject ( data ) ;
434
- var req = this . request ( ) ;
435
- var type = req . getHeader ( 'Content-Type' ) ;
436
-
437
+ var type = this . get ( 'Content-Type' ) ;
437
438
// merge
438
439
if ( obj && isObject ( this . _data ) ) {
439
440
for ( var key in data ) {
@@ -443,7 +444,7 @@ Request.prototype.send = function(data){
443
444
} else if ( 'string' == typeof data ) {
444
445
// default to x-www-form-urlencoded
445
446
if ( ! type ) this . type ( 'form' ) ;
446
- type = req . getHeader ( 'Content-Type' ) ;
447
+ type = this . get ( 'Content-Type' ) ;
447
448
448
449
// concat &
449
450
if ( 'application/x-www-form-urlencoded' == type ) {
@@ -637,6 +638,13 @@ Request.prototype.redirect = function(res){
637
638
638
639
delete this . req ;
639
640
641
+ // remove all add header except User-Agent
642
+ for ( var key in this . header ) {
643
+ if ( key !== 'User-Agent' ) {
644
+ delete this . header [ key ]
645
+ }
646
+ }
647
+
640
648
// redirect
641
649
this . url = url ;
642
650
this . _redirectList . push ( url ) ;
@@ -753,8 +761,9 @@ Request.prototype.request = function(){
753
761
// add cookies
754
762
if ( this . cookies ) req . setHeader ( 'Cookie' , this . cookies ) ;
755
763
756
- // set default UA
757
- req . setHeader ( 'User-Agent' , 'node-superagent/' + pkg . version ) ;
764
+ for ( var key in this . header ) {
765
+ req . setHeader ( key , this . header [ key ] ) ;
766
+ }
758
767
759
768
return req ;
760
769
} ;
@@ -861,7 +870,7 @@ Request.prototype.end = function(fn){
861
870
862
871
// content-length
863
872
if ( data && ! req . getHeader ( 'Content-Length' ) ) {
864
- this . set ( 'Content-Length' , Buffer . isBuffer ( data ) ? data . length : Buffer . byteLength ( data ) ) ;
873
+ req . setHeader ( 'Content-Length' , Buffer . isBuffer ( data ) ? data . length : Buffer . byteLength ( data ) ) ;
865
874
}
866
875
}
867
876
0 commit comments