响应头和设置的头信息不一致

原地起飞

问题描述

代码里面设置的 Transfer-Encoding:chunked
这里写问题描述

截图

        $ask = $request->input('ask');
        if(!$ask){
            return json(['code' => 1005,'msg' => '缺少必要参数']);
        }
        $apikey = '';
        $secretKey = '';
        $traceId = uniqid();//'A9AB29036A9D4E05545116DA32123dsf2';
        $timestamp = time();
        $url = 'xxxx' . $apikey;
        $http = new \Workerman\Http\Client();
        $connection = $request->connection;
        $bufferSend = new Response(200, ['Content-Type' => 'text/event-stream'],"\r\n");
        $connection->send($bufferSend,true);
        $data = [
            'traceId' => $traceId,
            'timestamp' => $timestamp,
            'stream' => true,
            'messages' => [
                [
                    'content' => $ask,
                    'role' => 'user'
                ]
            ],

        ];

        $http->request($url, [
            'method' => 'POST',
            'data' => json_encode($data),
            'headers' => [
                'Content-Type' => 'application/json',
                'App-Sign' => $this->makeXcSign($apikey,$secretKey,$traceId,$timestamp),
            ],
            'progress' => function($buffer) use ($connection) {
                $connection->send($buffer, true);
            },
            'success' => function($response) use ($connection) {
                $connection->send(new Chunk(''));
                $connection->close();
            },
        ]);

        return (new Response(200, [
            "Transfer-Encoding" => "chunked",
        ]));
159 2 0
2个回答

walkor

根据你的代码,http头是这块的代码发送的

$connection = $request->connection;
$bufferSend = new Response(200, ['Content-Type' => 'text/event-stream'],"\r\n");
$connection->send($bufferSend,true);
  • 原地起飞 2024-06-13

    我这边写到上面就不输入内容了,这个是什么原因呀

  • walkor 2024-06-13

    因为你发送了两个http头,是错误用法。

    $bufferSend = new Response(200, ['Content-Type' => 'text/event-stream'],"\r\n");
    $connection->send($bufferSend,true);

    这段去掉

  • 原地起飞 2024-06-13

    去掉的话就不是这个event-stream了

  • 原地起飞 2024-06-13

    加在下面的话报错

  • 原地起飞 2024-06-13

    能不能让content-type和这个Transfer-Encoding都改了呢

原地起飞

截图
改了后流数据没返回
截图

×
🔝