@@ -41,6 +41,7 @@ const kCancelTimeouts = Symbol('cancelTimeouts');
41
41
const kStartedReading = Symbol ( 'startedReading' ) ;
42
42
const kStopReading = Symbol ( 'stopReading' ) ;
43
43
const kTriggerRead = Symbol ( 'triggerRead' ) ;
44
+ const kBody = Symbol ( 'body' ) ;
44
45
export const kIsNormalizedAlready = Symbol ( 'isNormalizedAlready' ) ;
45
46
46
47
const supportsBrotli = is . string ( ( process . versions as any ) . brotli ) ;
@@ -452,6 +453,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
452
453
[ kUploadedSize ] : number ;
453
454
[ kStopReading ] : boolean ;
454
455
[ kTriggerRead ] : boolean ;
456
+ [ kBody ] : Options [ 'body' ] ;
455
457
[ kBodySize ] ?: number ;
456
458
[ kServerResponsesPiped ] : Set < ServerResponse > ;
457
459
[ kIsFromCache ] ?: boolean ;
@@ -889,21 +891,23 @@ export default class Request extends Duplex implements RequestEvents<Request> {
889
891
if ( isFormData ( options . body ) && noContentType ) {
890
892
headers [ 'content-type' ] = `multipart/form-data; boundary=${ options . body . getBoundary ( ) } ` ;
891
893
}
894
+
895
+ this [ kBody ] = options . body ;
892
896
} else if ( isForm ) {
893
897
if ( noContentType ) {
894
898
headers [ 'content-type' ] = 'application/x-www-form-urlencoded' ;
895
899
}
896
900
897
- options . body = ( new URLSearchParams ( options . form as Record < string , string > ) ) . toString ( ) ;
901
+ this [ kBody ] = ( new URLSearchParams ( options . form as Record < string , string > ) ) . toString ( ) ;
898
902
} else {
899
903
if ( noContentType ) {
900
904
headers [ 'content-type' ] = 'application/json' ;
901
905
}
902
906
903
- options . body = JSON . stringify ( options . json ) ;
907
+ this [ kBody ] = JSON . stringify ( options . json ) ;
904
908
}
905
909
906
- const uploadBodySize = await getBodySize ( options ) ;
910
+ const uploadBodySize = await getBodySize ( this [ kBody ] , options . headers ) ;
907
911
908
912
// See https://tools.ietf.org/html/rfc7230#section-3.3.2
909
913
// A user agent SHOULD send a Content-Length in a request message when
@@ -1149,21 +1153,23 @@ export default class Request extends Duplex implements RequestEvents<Request> {
1149
1153
this . emit ( 'uploadProgress' , this . uploadProgress ) ;
1150
1154
1151
1155
// Send body
1156
+ const body = this [ kBody ] ;
1152
1157
const currentRequest = this . redirects . length === 0 ? this : request ;
1153
- if ( is . nodeStream ( options . body ) ) {
1154
- options . body . pipe ( currentRequest ) ;
1155
- options . body . once ( 'error' , ( error : NodeJS . ErrnoException ) => {
1158
+
1159
+ if ( is . nodeStream ( body ) ) {
1160
+ body . pipe ( currentRequest ) ;
1161
+ body . once ( 'error' , ( error : NodeJS . ErrnoException ) => {
1156
1162
this . _beforeError ( new UploadError ( error , this ) ) ;
1157
1163
} ) ;
1158
1164
1159
- options . body . once ( 'end' , ( ) => {
1165
+ body . once ( 'end' , ( ) => {
1160
1166
delete options . body ;
1161
1167
} ) ;
1162
1168
} else {
1163
1169
this . _unlockWrite ( ) ;
1164
1170
1165
- if ( ! is . undefined ( options . body ) ) {
1166
- this . _writeRequest ( options . body , null as unknown as string , ( ) => { } ) ;
1171
+ if ( ! is . undefined ( body ) ) {
1172
+ this . _writeRequest ( body , null as unknown as string , ( ) => { } ) ;
1167
1173
currentRequest . end ( ) ;
1168
1174
1169
1175
this . _lockWrite ( ) ;
0 commit comments