@@ -151,12 +151,22 @@ class ServiceRelay {
151151 // that express does but we will use the content-length header to determine whether there will be content on the proxied request.
152152 if ( typeof req . body !== 'undefined' && req . body && typeof headers [ 'content-length' ] !== 'undefined' ) {
153153 let body = JSON . stringify ( req . body ) ;
154- logger . verbose ( `Writing request body: ${ body } ` ) ;
154+ const headerLen = ( req . headers [ 'content-length' ] || '' ) . toString ( ) ;
155+ logger . verbose ( `Evaluating request body for relay: length=${ body . length } headerLen=${ headerLen } ` ) ;
155156 if ( body && body . length > 0 ) {
156- if ( body . length . toString ( ) !== req . headers [ 'content-length' ] ) logger . warn ( `The content length header is incorrect for ${ uri . href } : Body: ${ body . length } !== Content: ${ req . headers [ 'content-length' ] } ` ) ;
157- reqProxy . write ( body , ( err ) => {
158- if ( err ) logger . error ( `Error writing response body: ${ uri . href } : ${ err . message } ` ) ;
159- } ) ;
157+ // If the original header declared 0 length but express/json parser produced an empty object/array, skip sending body.
158+ if ( headerLen === '0' && ( body === '{}' || body === '[]' ) ) {
159+ logger . verbose ( `Suppressing empty JSON body for ${ uri . href } (header length 0, derived length ${ body . length } ).` ) ;
160+ }
161+ else {
162+ if ( body . length . toString ( ) !== headerLen && headerLen !== '0' ) {
163+ // Downgrade to debug to avoid noisy warnings for harmless mismatches; ideally we would adjust the header earlier.
164+ logger . debug ( `Content-Length mismatch for ${ uri . href } : Body: ${ body . length } !== Header: ${ headerLen } ` ) ;
165+ }
166+ reqProxy . write ( body , ( err ) => {
167+ if ( err ) logger . error ( `Error writing response body: ${ uri . href } : ${ err . message } ` ) ;
168+ } ) ;
169+ }
160170 }
161171 }
162172 reqProxy . on ( 'error' , ( err ) => {
0 commit comments