繁体   English   中英

为什么我从 api 收到 400 Bad Request 错误

[英]Why do I get a 400 Bad Request error from api

我正在尝试调用 shiplogic API(基于 AWS),但出现400 Bad Request错误。

我尝试了各种方法来请求来自 AWS API(例如 S3)的响应,我尝试为 cURL 请求创建自己的签名,现在我正在尝试此代码。

$host = "api.shiplogic.com";
$accessKey = 'AKIA55D****';
$secretKey = 'cx0WDJLNj1Bmn2**';
$requestUrl = 'https://api.shiplogic.com';
$uri = '/tracking/shipments';
$httpRequestMethod = 'GET';
$data = '{"tracking_reference": "M3RPH"}';

require 'AWS/aws-autoloader.php';;
use Aws\Signature\SignatureV4;
use Aws\Credentials\Credentials;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;

$signature = new SignatureV4('execute-api', 'af-south-1');
$credentials = new Credentials($accessKey, $secretKey);
$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri, ["content-type"=>"application/json"], $data);
$client = new Client([$requestUrl, 'timeout' => 30]);
$sr = $signature->signRequest($psr7Request, $credentials);
$response = $client->send($sr);
var_dump($response);

简短的错误是: PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://api.shiplogic.com/tracking/shipments resulted in a 400 Bad Request response: Invalid or incomplete input

完整的错误是:

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://api.shiplogic.com/tracking/shipments` resulted in a `400 Bad Request` response:
Invalid or incomplete input

in /home/path/myurl.com/path/AWS/GuzzleHttp/Exception/RequestException.php:113

Stack trace:
#0 /home/path/myurl.com/path/AWS/GuzzleHttp/Middleware.php(65): GuzzleHttp\Exception\RequestException::create()
#1 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}()
#2 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler()
#3 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#4 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run()
#5 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn()
#6 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending()
#7 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList()
#8 /home/path/myurl.com/path/AWS/GuzzleHttp/Promise/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending()
#9 /home/path/myurl.com/path/AWS/GuzzleHttp/Client.php(129): GuzzleHttp\Promise\Promise->wait()
#10 /home/path/myurl.com/path/apiSign.php(22): GuzzleHttp\Client->send()
#11 {main}
thrown in /home/path/myurl.com/path/AWS/GuzzleHttp/Exception/RequestException.php on line 113

编辑:

Postman创建的cURL截图

在此处输入图像描述

编辑 2

使用$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri.'?tracking_reference=M3RPH');

GuzzleHttp\Psr7\Response Object
(
[reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
[statusCode:GuzzleHttp\Psr7\Response:private] => 200
[headers:GuzzleHttp\Psr7\Response:private] => Array
    (
        [Date] => Array
            (
                [0] => Sat, 30 Jul 2022 09:08:57 GMT
            )

        [Content-Type] => Array
            (
                [0] => application/json
            )

        [Content-Length] => Array
            (
                [0] => 743
            )

        [Connection] => Array
            (
                [0] => keep-alive
            )

        [Ship-Logic-Request-Id] => Array
            (
                [0] => e78565f7-3a63-4831-8cdd-f4c720c13f06
            )

        [Apigw-Requestid] => Array
            (
                [0] => WEs-jjI6ifMEPcQ=
            )

    )

[headerNames:GuzzleHttp\Psr7\Response:private] => Array
    (
        [date] => Date
        [content-type] => Content-Type
        [content-length] => Content-Length
        [connection] => Connection
        [ship-logic-request-id] => Ship-Logic-Request-Id
        [apigw-requestid] => Apigw-Requestid
    )

[protocol:GuzzleHttp\Psr7\Response:private] => 1.1
[stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream  Object
    (
        [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #45
        [size:GuzzleHttp\Psr7\Stream:private] => 
        [seekable:GuzzleHttp\Psr7\Stream:private] => 1
        [readable:GuzzleHttp\Psr7\Stream:private] => 1
        [writable:GuzzleHttp\Psr7\Stream:private] => 1
        [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
        [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
            (
            )

    )

)

POSTMAN 来自同一调用的响应:

{
"shipments": [
    {
        "provider_id": 10,
        "shipment_id": 14135,
        "short_tracking_reference": "G9G",
        "status": "cancelled",
        "shipment_time_created": "2021-05-21T12:12:03.365338Z",
        "shipment_time_modified": "2022-05-31T14:00:34.375688Z",
        "shipment_collected_date": null,
        "shipment_delivered_date": null,
        "collection_from": "uAfrica.com",
        "delivery_to": "",
        "collection_hub": "Gauteng",
        "delivery_hub": "Gauteng",
        "service_level_code": "ECO",
        "tracking_events": [
            {
                "id": 1940829,
                "parcel_id": 0,
                "date": "2021-11-22T14:21:00.000529Z",
                "status": "delivered",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940828,
                "parcel_id": 0,
                "date": "2021-11-22T14:20:59.925068Z",
                "status": "collected",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940812,
                "parcel_id": 0,
                "date": "2021-11-22T14:20:45.323472Z",
                "status": "delivered",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940811,
                "parcel_id": 0,
                "date": "2021-11-22T14:20:45.317556Z",
                "status": "collected",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940755,
                "parcel_id": 0,
                "date": "2021-11-22T14:19:12.481537Z",
                "status": "delivered",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940754,
                "parcel_id": 0,
                "date": "2021-11-22T14:19:12.462331Z",
                "status": "collected",
                "source": "corneladmin",
                "message": ""
            },
            {
                "id": 1940726,
                "parcel_id": 0,
                "date": "2021-11-22T14:18:37.02554Z",
                "status": "delivered",
                "source": "sandbox-admin",
                "lat": -25.806655,
                "lng": 28.334732,
                "message": "POD files captured"
            },
            {
                "id": 1940725,
                "parcel_id": 0,
                "date": "2021-11-22T14:18:37.011858Z",
                "status": "collected",
                "source": "sandbox-admin",
                "message": ""
            },
            {
                "id": 1940713,
                "parcel_id": 0,
                "date": "2021-11-22T14:18:20.002241Z",
                "status": "delivered",
                "source": "sandbox-admin",
                "lat": -25.806655,
                "lng": 28.334732,
                "message": "POD files captured"
            },
            {
                "id": 1940712,
                "parcel_id": 0,
                "date": "2021-11-22T14:18:19.995663Z",
                "status": "collected",
                "source": "sandbox-admin",
                "message": ""
            },
            {
                "id": 332828,
                "parcel_id": 0,
                "date": "2021-06-04T09:30:36.170053Z",
                "status": "cancelled",
                "source": "sandbox-admin",
                "message": ""
            },
            {
                "id": 254076,
                "parcel_id": 0,
                "date": "2021-05-21T12:12:03.846219Z",
                "status": "submitted",
                "source": "sandbox-admin",
                "message": ""
            }
        ]
    }
]
} 

文档说您需要在查询字符串上传递tracking_reference参数(就像您对 Postman 所做的那样):

https://api.shiplogic.com/tracking/shipments?tracking_reference=G9G

但这不是您的代码所做的:

$data = '{"tracking_reference": "M3RPH"}';
$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri, ["content-type"=>"application/json"], $data)

异常消息清楚地表明 URL 中缺少该参数:

`GET https://api.shiplogic.com/tracking/shipments` resulted in a `400 Bad Request` response

你应该这样做:

$psr7Request = new Request($httpRequestMethod, $requestUrl.$uri.'?tracking_reference=M3RPH');

我在使用 Creatio API 时遇到了同样的问题。 就像您的情况一样,API 在 Postman 中工作,但在 Z2FEC392304A5C23AC138DA2274 中使用 Curl 时却没有。 有时 postman 在 HTTP 请求中发送一些额外的标头。 在我的例子中,postman 匿名发送了一个cookie header。 所以要检查这一点:导航到出现在应用程序右侧的 Postman 的代码部分。 选择 PHP 作为编程语言,它将为您显示完整的Curl请求。

在此处输入图像描述 在此处输入图像描述

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM