gpt4 book ai didi

php - Guzzle curl 请求得到空响应正文,但状态为 200 且 Content-Length header 正确

转载 作者:可可西里 更新时间:2023-11-01 16:42:23 29 4
gpt4 key购买 nike

我正在通过 Guzzle 3.8.1 发出超过 2MB 的 Jasper 报告请求(通过 Jasper Server API),我收到的响应带有正确的 Content-Length header ,但没有响应正文。

Guzzle 请求:

GET /jasperserver/rest_v2/reports/projects/i3app_suite/Resource/BulkShiftExport.csv?ACCOUNT_ID=2&START_DATETIME=2015-01-01&END_DATETIME=2015-01-31 HTTP/1.1
Host: jasper.i3app:8080
User-Agent: Guzzle/3.8.1 curl/7.19.7 PHP/5.5.8
Authorization: Basic ***=

响应:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: private
Expires: Wed, 31 Dec 1969 17:00:00 MST
P3P: CP="ALL"
Set-Cookie: JSESSIONID=F0B0F72B65A8145B45DA9DB2BACE53D8; Path=/jasperserver/; HttpOnly, userLocale=en_US;Expires=Fri, 13-Feb-2015 18:56:44 GMT;HttpOnly
Content-Disposition: attachment; filename="BulkShiftExport.csv"
output-final: true
Content-Type: application/vnd.ms-excel
Content-Length: 2173897
Date: Thu, 12 Feb 2015 18:57:02 GMT

如果我通过命令行上的 curl 发出此请求(或在浏览器中请求),我会按预期收到报告

GET /jasperserver/rest_v2/reports/projects/i3app_suite/Resource/BulkShiftExport.csv?ACCOUNT_ID=2&START_DATETIME=2015-01-01&END_DATETIME=2015-01-30 HTTP/1.1
Authorization: Basic ***=
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: jasper.i3app:8080
Accept: */*


< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Cache-Control: private
< Expires: Wed, 31 Dec 1969 17:00:00 MST
< P3P: CP="ALL"
< Set-Cookie: JSESSIONID=AF1BF885354AF3E352DD9E18FA044A4B; Path=/jasperserver/; HttpOnly
< Set-Cookie: userLocale=en_US;Expires=Fri, 13-Feb-2015 19:03:42 GMT;HttpOnly
< Content-Disposition: attachment; filename="BulkShiftExport.csv"
< output-final: true
< Content-Type: application/vnd.ms-excel
< Content-Length: 2113902
< Date: Thu, 12 Feb 2015 19:03:49 GMT
<
{ [data not shown]

我能看到的唯一区别是 curl 请求中的 Accept: */*。我尝试将该 header 添加到 guzzle 请求并得到相同的结果。

当通过 Guzzle 客户端发出请求时,似乎需要相同的时间(5-6 秒)来接收响应,并且它设置了 Content-Length header ,但响应正文为空。为什么我通过使用 curl 的 Guzzle 得到一个空的响应主体,但在命令行上使用 curl 时却没有?是否需要设置一个选项才能使其正常工作?

    $request = $this->getGuzzleClient()->createRequest('GET');

$config = $this->getConfig();
$url = new Url(
$config['scheme'],
$config['host'],
$config['user'],
$config['pass'],
$config['port'],
$config['path'] . $reportPath . '.' . $format,
new QueryString($parameters)
);
$request->setUrl($url);

$response = $request->send();

...

public function getGuzzleClient()
{
if (!$this->restClient) {
$client = new GuzzleClient();
$this->setRestClient($client);
}
return $this->restClient;
}

最佳答案

在我的例子中,我错误地使用了 MockHandler

关于php - Guzzle curl 请求得到空响应正文,但状态为 200 且 Content-Length header 正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485842/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com