I’m trying to POST data to a REST API.
On Postman I can POST with success, the JSON body is:
{ "version": 0, "roles": { "customer": { } }, "person": { "firstName": "Inge", "lastName": "Musterfrau" }, "emailAddresses" :{ "private" : [ "email@domain.com" ] }, "addresses": { "billing": [ { "supplement": null, "street": "aaa", "zip": "12345", "city": "Berlin", "countryCode": "DE" } ] } }
My problem is on addresses billing. I don’t know how to create the object/array correctly so the API accept it.
I’m build the parameters on PHO like bellow:
$billingAddr = array( "supplement" => $billingAddress["streetDetails"], "street" => $billingAddress["street"], "zip" => $billingAddress["zipCode"], "city" => $billingAddress["city"], "countryCode" => $billingAddress["country"], ); $params = [ "version" => 0, "roles" => $roles, "person" => $person, "emailAddresses" => $emailAddresses, "addresses" => [ "billing" => $billingAddr ] ];
I get an error: “missing_entity – addresses – validation_failure”.
I believe my issue is creating the mixed Object addresses, array billing.
Advertisement
Answer
So, going strictly by the PHP example, doing a json_encode
‘d output of that shows that the structure is not quite the same at the addresses.
Note that I do not have the data for most of the rest of the json info from the PHP example, so the output examples below are strictly focused on the addresses
section.
Before JSON change: echo json_encode($params, JSON_PRETTY_PRINT); { "version": 0, "roles": null, "person": null, "emailAddresses": null, "addresses": { "billing": { "supplement": null, "street": null, "zip": null, "city": null, "countryCode": null } } }
… in that, note that the billing does not have the []
characters like what is sent using postman.
No worries though, it’s an easy fix. The addresses part should be changed to get an array of billing addresses, like so:
"addresses" => [ "billing" => [$billingAddr] ]
Then running a recheck with that change applied shows:
After JSON change: echo json_encode($params, JSON_PRETTY_PRINT); { "version": 0, "roles": null, "person": null, "emailAddresses": null, "addresses": { "billing": [ { "supplement": null, "street": null, "zip": null, "city": null, "countryCode": null } ] } }
That should help to achieve the expected format.
Here’s the fiddle example in case you might need another PHP version to check. The fiddle default is 8.0.1