![](/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.