workerman/http-client 设置 Content-Type,Bug

veinliu
POST /autoPalletCAN?pEvent=openBagSignal HTTP/1.1
Host: 192.168.11.1:9000
User-Agent: workerman/http-client
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded, application/json
Content-Length: 627

RT
如果设置了的话Content-Type 会出现多个值,在某些接口上会报错。
上面是抓色的结果。要发送的Content-Type 是 application/json 但是会出现多个值。
用是这么用的

$http1->request($url, [
                'method'  => 'POST',
                'version' => '1.1',
                'headers' => ['Connection' => 'keep-alive'],
                'headers' => ['Content-type'=>'application/json'],
                'data'    => [$post],
                'success' => function ($response) {
                    echo $response->getBody();
                },
                'error'   => function ($exception) {
                    $ident = 'SAC2WorkStation';
                    new logErr($ident,$exception);
                }
            ]);

上次设置Connection好像出有类似问题。

2947 2 0
2个回答

veinliu

Request.php 这样改一下解决了。

public function end($data = '')
    {
        if (($data || $data === '0' || $data === 0) || $this->getBody()->getSize()) {
            if (!isset($this->_options['headers']['Content-Type'])) {
                $this->withHeader('Content-Type', 'application/x-www-form-urlencoded');
            }
            $this->withHeader('Content-Type', $this->_options['headers']['Content-Type']);
        }
  • 暂无评论
walkor 打赏

已经在v0.1.3版本修复

  • 暂无评论
年代过于久远,无法发表回答
×
🔝