![](/img/trans.png)
[英]Axios POST to Laravel API results in 419 error but works fine in postman
[英]400 Error on API endpoint in Guzzle that works fine in Browser & Postman
我正在尝试访问以下公共API资源:
当我在浏览器中尝试时,它会以JSON文件下载。 当我在Postman中尝试时,它显示为文本(JSON格式)。
当我在Guzzle中尝试时,出现400错误。
$apiResource = "http://www.nomisweb.co.uk/api/v01/dataset/NM_17_5.data.json?geography=1946157081,943718401...943718512,2092957698&date=latest&variable=18&measures=20599,21001,21002,21003";
try {
$client = new Client();
$res = $client->request('GET', $apiResource);
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {
die($e->getMessage());
}
我怀疑问题与API返回
Content-Disposition attachment
在标题中,但我不知道Guzzle处理此问题的正确方法是什么。
为了清楚起见,我想获取原始文本输出,而不是将文件作为附件。
您需要做的就是获取响应的主体 (将其放入Stream对象中),然后获取该响应的内容:
$apiResource = "http://www.nomisweb.co.uk/api/v01/dataset/NM_17_5.data.json?geography=1946157081,943718401...943718512,2092957698&date=latest&variable=18&measures=20599,21001,21002,21003";
try {
$client = new Client();
$res = $client->request('GET', $apiResource)->getBody()->getContents();
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {
die($e->getMessage());
}
编辑:
用于测试的确切代码:
Route::get('/guzzletest', function() {
$apiResource = "http://www.nomisweb.co.uk/api/v01/dataset/NM_17_5.data.json?geography=1946157081,943718401...943718512,2092957698&date=latest&variable=18&measures=20599,21001,21002,21003";
try {
// use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('GET', $apiResource)->getBody()->getContents();
dd($res);
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {
die($e->getMessage());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.