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
There are two things that I can see going wrong.
You want to pass the following json
but the php array
will generate
instead.
To fix this, simply change that part by
To fix that, you can use the
Http
facade’sasJson
method.[
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
},
{
"name": "Jane Smith",
"email": "[email protected]",
"password": "password456"
}
]