【问题标题】:Unexpected token while deserializing object: when using Click and Drop API - Royal Mail反序列化对象时出现意外令牌:使用 Click and Drop API 时 - Royal Mail
【发布时间】:2021-05-18 16:31:24
【问题描述】:

我正在发出 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" => "test@test.com"
                ],
                "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" => "test@test.com"
                ],
                "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 客户端。

【问题讨论】:

  • 它不喜欢你的地址。您必须发布您发送的内容,以便我们了解原因。
  • 抱歉,我已经用 POST 请求有效负载的完整示例更新了原始帖子。

标签: php laravel guzzle


【解决方案1】:

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" => "test@test.com"
            ],
            "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" => "test@test.com"
            ],
            "orderDate" => "2021-05-18T16:39:01Z",
            "subtotal" => 0,
            "shippingCostCharged" => 0,
            "total" => 0,
        ]]
    ])->json();

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2012-01-09
    • 2023-03-11
    • 2022-06-12
    • 2011-09-20
    • 2011-11-22
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多