skip to Main Content

I have a problem with calling an API with Laravel.
I wont to call an api with a request like this (this is an example of json request):

{
   "costCenterCode":"CDC-00070964",
   "paperless":"false",
   "shipmentDate":"2020-11-26T08:02:20.986+0000",
   "waybills":[
      {
         "clientReferenceId":"12312312341",
         "printFormat":"A4",
         "product":"APT000901",
         "data":{
            "declared":[
               {
                  "weight":"10",
                  "height":"10",
                  "length":"30",
                  "width":"25"
               }
            ],
         }
      }
   ]
}

This is my post code:

$insert = Http::withHeaders([
        'POSTE_clientID' => '4815fd18-xxxx-xxxx-xxxx-d1a1edbd7a12',
        'Authorization' => $auth,
    ])
    ->post('https://apiw.gp.posteitaliane.it/gp/internet/postalandlogistics/parcel/waybill', [
        "costCenterCode" => "CDC-00051975",
        "paperless" => "false",
        "shipmentDate" => "2024-06-29T08=>02=>20.986+0000",
        "waybills" => [
            "clientReferenceId" => "1404343485",
            "printFormat" => "A4",
            "product" => "APT000901",
            "data" => [
                "declared" => [
                    "weight" => "0",
                    "height" => "44",
                    "length" => "30",
                    "width" => "25"
                ],
            ]
        ]
    ])
    ->json();

What am I doing wrong? There is an array of object, I don’t know.
Can you help me?

2

Answers


  1. There are two things that I can see going wrong.

    1. The array is not properly formatted.

    You want to pass the following json

    ...
    "waybills": [
        {
            "clientReferenceId":"12312312341",
            "printFormat":"A4",
            "product":"APT000901",
            "data": {
                "declared": [
                    {
                        "weight":"10",
                        "height":"10",
                        "length":"30",
                        "width":"25"
                    }
                ],
            }
        }
    ],
    ...
    

    but the php array

    ...
    "waybills" => [
        "clientReferenceId" => "1404343485",
        "printFormat" => "A4",
        "product" => "APT000901",
        "data" => [
            "declared" => [
                "weight" => "0",
                "height" => "44",
                "length" => "30",
                "width" => "25"
            ],
        ]
    ],
    ...
    

    will generate

    ...
    "waybills": {
        "clientReferenceId":"12312312341",
        "printFormat":"A4",
        "product":"APT000901",
        "data": {
            "declared": [
                {
                    "weight":"10",
                    "height":"10",
                    "length":"30",
                    "width":"25"
                }
            ],
        }
    }
    ...
    

    instead.

    To fix this, simply change that part by

    ...
    "waybills" => [
        [
            "clientReferenceId" => "1404343485",
            "printFormat" => "A4",
            "product" => "APT000901",
            "data" => [
                "declared" => [
                    "weight" => "0",
                    "height" => "44",
                    "length" => "30",
                    "width" => "25"
                ],
            ]
        ]
    ]
    ...
    
    1. You say you are calling an API but you’re not specifying any json headers in the request.

    To fix that, you can use the Http facade’s asJson method.

    $insert = Http::asJson()
        ->withHeaders([
            'POSTE_clientID' => '4815fd18-xxxx-xxxx-xxxx-d1a1edbd7a12',
            'Authorization' => $auth,
        ])
        ->post('https://apiw.gp.posteitaliane.it/gp/internet/postalandlogistics/parcel/waybill', [
            "costCenterCode" => "CDC-00051975",
            "paperless" => "false",
            "shipmentDate" => "2024-06-29T08=>02=>20.986+0000",
            "waybills" => [
                [
                    "clientReferenceId" => "1404343485",
                    "printFormat" => "A4",
                    "product" => "APT000901",
                    "data" => [
                        "declared" => [
                            "weight" => "0",
                            "height" => "44",
                            "length" => "30",
                            "width" => "25"
                        ],
                    ]
                ]
            ]
        ])
        ->json();
    
    Login or Signup to reply.
  2. [
    {
    "name": "John Doe",
    "email": "[email protected]",
    "password": "password123"
    },
    {
    "name": "Jane Smith",
    "email": "[email protected]",
    "password": "password456"
    }
    ]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search