phplaravelguzzle

Unexpected token while deserializing object: when using Click and Drop API - Royal Mail


I am making a POST request to create an order for 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);

but keep getting

'Unexpected token while deserializing object: PropertyName. Path 'items.recipient', line 1, position 22. Failed to deserialize following order request'

I keep getting the same error for all required fields...

API docs do not provide much details https://api.parcel.royalmail.com/. The same payload works in Insomnia. I am using Laravel Http client.


Solution

  • The API shows that items is an array of objects.

    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": {}
    }
    ]
    

    So you just need to wrap everything in items in another set of brackets.

    $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();