反序列化对象时出现意外令牌:使用 Click and Drop API 时 - Royal Mail

问题描述 投票:0回答:2

我正在发出 POST 请求以创建 Royal Mail Click and Drop 订单:

$response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer secret-123'
        ])->post('https://api.parcel.royalmail.com/api/v1/orders/', [
            'items' => [
                'recipient' => [
                    'address' => [
                        "fullName" => 'Tom',
                        "companyName" => "Test",
                        "addressLine1" => "150",
                        "addressLine2" => "Valley Close",
                        "addressLine3" => "Elmdom",
                        "city" => "Birmingham",
                        "county" => "West Midlands",
                        "postcode" => "B12 2YT",
                        "countryCode" => "GB"
                    ],
                    "emailAddress" => "[email protected]"
                ],
                "billing" => [
                    "address" => [
                        "fullName" => 'Tom',
                        "companyName" => "Test",
                        "addressLine1" => "150",
                        "addressLine2" => "Valley Close",
                        "addressLine3" => "Elmdom",
                        "city" => "Birmingham",
                        "county" => "West Midlands",
                        "postcode" => "B12 2YT",
                        "countryCode" => "GB"
                    ],
                    "phoneNumber" => "42425 5252552",
                    "emailAddress" => "[email protected]"
                ],
                "orderDate" => "2021-05-18T16:39:01Z",
                "subtotal" => 0,
                "shippingCostCharged" => 0,
                "total" => 0,
            ]
        ])->json();
        dd($response);

但继续获得

'反序列化对象时出现意外标记:PropertyName。路径“items.recipient”,第 1 行,位置 22。无法反序列化以下订单请求”

我在所有必填字段中不断收到相同的错误...

API 文档没有提供太多详细信息https://api.parcel.royalmail.com/。同样的有效负载也适用于 Insomnia。我正在使用 Laravel Http 客户端。

php laravel guzzle
2个回答
0
投票

API 显示

items
是一个对象数组。

items": [
{
"orderReference": "string",
"recipient": {},
"sender": {},
"billing": {},
"packages": [],
"orderDate": "2019-08-24T14:15:22Z",
"plannedDespatchDate": "2019-08-24T14:15:22Z",
"specialInstructions": "string",
"subtotal": 0,
"shippingCostCharged": 0,
"otherCosts": 0,
"customsDutyCosts": 0,
"total": 0,
"currencyCode": "str",
"postageDetails": {},
"tags": [],
"label": {}
}
]

因此,您只需将所有内容包装在另一组括号中的项目中即可。

$response = Http::withHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer secret-123'
    ])->post('https://api.parcel.royalmail.com/api/v1/orders/', [
        'items' => [[
            'recipient' => [
                'address' => [
                    "fullName" => 'Tom',
                    "companyName" => "Test",
                    "addressLine1" => "150",
                    "addressLine2" => "Valley Close",
                    "addressLine3" => "Elmdom",
                    "city" => "Birmingham",
                    "county" => "West Midlands",
                    "postcode" => "B12 2YT",
                    "countryCode" => "GB"
                ],
                "emailAddress" => "[email protected]"
            ],
            "billing" => [
                "address" => [
                    "fullName" => 'Tom',
                    "companyName" => "Test",
                    "addressLine1" => "150",
                    "addressLine2" => "Valley Close",
                    "addressLine3" => "Elmdom",
                    "city" => "Birmingham",
                    "county" => "West Midlands",
                    "postcode" => "B12 2YT",
                    "countryCode" => "GB"
                ],
                "phoneNumber" => "42425 5252552",
                "emailAddress" => "[email protected]"
            ],
            "orderDate" => "2021-05-18T16:39:01Z",
            "subtotal" => 0,
            "shippingCostCharged" => 0,
            "total" => 0,
        ]]
    ])->json();

0
投票

您是如何获得皇家邮政的授权的?

© www.soinside.com 2019 - 2024. All rights reserved.