[英]400 Error on API endpoint in Guzzle that works fine in Browser & Postman
I am trying to access the following public API resource: 我正在尝试访问以下公共API资源:
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 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
When I try it in the browser, it downloads as a JSON file. 当我在浏览器中尝试时,它会以JSON文件下载。 When I try it in Postman, it shows as text (JSON format).
当我在Postman中尝试时,它显示为文本(JSON格式)。
When I try it in Guzzle, I get a 400 error. 当我在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());
}
I suspect the problem is something to do with the API returning the 我怀疑问题与API返回
Content-Disposition attachment
In the header, but I don't know what the correct way for Guzzle to handle this is. 在标题中,但我不知道Guzzle处理此问题的正确方法是什么。
Just to be clear, I want to get the raw text output and not the file as an attachment. 为了清楚起见,我想获取原始文本输出,而不是将文件作为附件。
All you need to do is get the body of the response (which is put into a Stream object) and then get the contents of that response: 您需要做的就是获取响应的主体 (将其放入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());
}
Edit: 编辑:
Exact code used for testing: 用于测试的确切代码:
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.