简体   繁体   English

Guzzle中API端点上的400错误,在浏览器和邮递员中可以正常使用

[英]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.

相关问题 Axios POST 到 Laravel API 导致 419 错误,但在邮递员中工作正常 - Axios POST to Laravel API results in 419 error but works fine in postman 修复Guzzle 400错误的API错误请求 - Fixing the Guzzle 400 Bad request for an API error API 在浏览器中有效,但在邮递员或应用程序中无效 - API works in browser but not in postman or app 对API的请求在Postman中有效,但在我尝试使用laravel / guzzle时无效 - Request to API works in Postman but not when I try with laravel/guzzle POST Request适用于Postman,但不适用于Guzzle - POST Request works with Postman, but not with Guzzle Flutter 中的 401 未授权错误,但在 Postman 中工作正常 - 401 unauthorized error in Flutter but works fine in Postman 当 curl 和 Postman 完全正常时,Guzzle 错误“调用成员 function getStatusCode() on null” - Guzzle error “Call to a member function getStatusCode() on null” when curl and Postman are perfectly fine Guzzle 身份验证返回 400 错误 - Guzzle authentication returning 400 error Laravel API 端点“401 Unauthorized”在服务器上但在 Localhost 上工作正常 - Laravel API Endpoint "401 Unauthorized" on Server But Works Fine On Localhost Guzzle 7 - 403 Forbidden(适用于 CURL) - Guzzle 7 - 403 Forbidden (works fine with CURL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM