guzzle / psr7

PSR-7 HTTP message library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Message::toString throw error when headers is list array

weskiller opened this issue · comments

PHP version: x.y.z (hint: php --version)
8.2
Description

when pass headers request option with list not assoc array,GuzzleHttp\Psr7\Message::toString will throws error
How to reproduce

<?php

use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

require  __DIR__ .'/vendor/autoload.php';

$middleware = static function (callable $handler)  {
    return static function (Request $request, array $options) use ($handler) {
        return $handler($request, $options)->then(
            static function (Response $response) use ($request) {
                Message::toString($request);
                Message::toString($response);
            },
        );
    };
};

$stack = HandlerStack::create(new CurlHandler()); // Wrap w/ middleware
$stack->push($middleware,'log');
$client = new GuzzleHttp\Client(['handler' => $stack]);
$response = $client->post('https://www.baidu.com',[
    'headers' => [
        'Content-Type: text/plain'     //sure, it's work
    ],
    'body' => 'GuzzleHttp\Client',
]);
shell#php test.php
PHP Fatal error:  Uncaught TypeError: strtolower(): Argument #1 ($string) must be of type string, int given in /test/vendor/guzzlehttp/psr7/src/Message.php:36
Stack trace:
#0 /test/vendor/guzzlehttp/psr7/src/Message.php(36): strtolower()
#1 /test/test.php(16): GuzzleHttp\Psr7\Message::toString()
#2 /test/vendor/guzzlehttp/promises/src/FulfilledPromise.php(48): {closure}()
#3 /test/vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\Promise\FulfilledPromise::GuzzleHttp\Promise\{closure}()
#4 /test/vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\Promise\TaskQueue->run()
#5 /test/vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn()
#6 /test/vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending()
#7 /test/vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList()
#8 /test/vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending()
#9 /test/vendor/guzzlehttp/guzzle/src/Client.php(189): GuzzleHttp\Promise\Promise->wait()
#10 /test/vendor/guzzlehttp/guzzle/src/ClientTrait.php(95): GuzzleHttp\Client->request()
#11 /test/test.php(26): GuzzleHttp\Client->post()
#12 {main}
  thrown in /test/vendor/guzzlehttp/psr7/src/Message.php on line 36

This is because you are passing an invalid header value. 'Content-Type: text/plain' must be 'Content-Type' => 'text/plain'.