@@ -379,16 +379,50 @@ public function request($path, array $options = []): RequestOperationResult
379379 } else {
380380 $ uri = $ path ;
381381 }
382- $ body = $ debug_body = null ;
382+ $ actual_guzzle_request_constructor_body = null ; // Body for new \GuzzleHttp\Psr7\Request()
383+ $ payload_for_logging_raw = null ; // Raw payload to be processed for logging
384+
383385 if (isset ($ options ['form_params ' ])) {
384- $ debug_body = $ this -> stripSensitiveInfo ( $ options ['form_params ' ]);
385- $ body = json_encode (
386+ $ payload_for_logging_raw = $ options ['form_params ' ]; // This is an array
387+ $ actual_guzzle_request_constructor_body = json_encode (
386388 $ options ['form_params ' ],
387389 JSON_UNESCAPED_SLASHES
388390 );
391+ // Unset form_params as we've processed it into $actual_guzzle_request_constructor_body
392+ // Guzzle's send() method should not also see $options['form_params'] if we provide a body to the Request constructor.
389393 unset($ options ['form_params ' ]);
390394 $ headers ['Content-Type ' ] = 'application/json ' ;
391- $ headers ['Content-Length ' ] = strlen ($ body );
395+ // Content-Length will be set by Guzzle or the HTTP client based on the final body.
396+ } elseif (isset ($ options ['json ' ])) {
397+ $ payload_for_logging_raw = $ options ['json ' ]; // This is an array
398+ // $actual_guzzle_request_constructor_body remains null. Guzzle's send() will use $options['json'].
399+ } elseif (isset ($ options ['body ' ])) {
400+ if (is_string ($ options ['body ' ])) {
401+ // Attempt to decode if it's a JSON string, for better stripping if it's an array.
402+ $ decoded_json_body = json_decode ($ options ['body ' ], true );
403+ if (json_last_error () === JSON_ERROR_NONE && is_array ($ decoded_json_body )) {
404+ $ payload_for_logging_raw = $ decoded_json_body ;
405+ } else {
406+ $ payload_for_logging_raw = $ options ['body ' ]; // Log as string
407+ }
408+ } elseif (is_resource ($ options ['body ' ]) || $ options ['body ' ] instanceof \Psr \Http \Message \StreamInterface) {
409+ $ payload_for_logging_raw = '[Stream/Resource Body] ' ;
410+ } else {
411+ $ payload_for_logging_raw = '[Unknown Body Type] ' ;
412+ }
413+ // $actual_guzzle_request_constructor_body remains null. Guzzle's send() will use $options['body'].
414+ }
415+
416+ $ logged_body_json_string = 'null ' ; // Default for logging if no body content
417+ if (is_array ($ payload_for_logging_raw )) {
418+ $ logged_body_json_string = json_encode (
419+ $ this ->stripSensitiveInfo ($ payload_for_logging_raw ),
420+ JSON_UNESCAPED_SLASHES
421+ );
422+ } elseif (is_string ($ payload_for_logging_raw )) {
423+ // For strings (e.g. pre-encoded JSON string not decoded to array, or placeholders)
424+ // We json_encode it to ensure it's a valid JSON value within the overall log JSON.
425+ $ logged_body_json_string = json_encode ($ payload_for_logging_raw , JSON_UNESCAPED_SLASHES );
392426 }
393427
394428 $ method = isset ($ options ['method ' ]) ? strtoupper (
@@ -403,10 +437,7 @@ public function request($path, array $options = []): RequestOperationResult
403437 ),
404438 'uri ' => $ uri ,
405439 'method ' => $ method ,
406- 'body ' => json_encode (
407- $ this ->stripSensitiveInfo ($ debug_body ),
408- JSON_UNESCAPED_SLASHES
409- ),
440+ 'body ' => $ logged_body_json_string , // Use the new comprehensive body logging
410441 ]
411442 );
412443 //Required objects and arrays stir benign warnings.
@@ -416,9 +447,9 @@ public function request($path, array $options = []): RequestOperationResult
416447 $ method ,
417448 $ uri ,
418449 $ headers ,
419- $ body
450+ $ actual_guzzle_request_constructor_body // Use the body derived from form_params, or null
420451 ),
421- $ options
452+ $ options // Guzzle will use $options['json'] or $options['body'] if $actual_guzzle_request_constructor_body is null
422453 );
423454 $ body = $ response ->getBody ()->getContents ();
424455 $ statusCode = $ response ->getStatusCode ();
0 commit comments