Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Andi1982's avatar

Anybody using API Resources as API request instead of just responses?

Hey,

I am wondering why there are just examples on how to return API-Resources as a response of a request.

I use resources to build the structure for some outgoing API-POST-requests and it is always very difficult to get the correct formated json into a variable that I could give to the http-client.

For example I have to send some configuration to some API via post request. Then I just do

$data = new ConfigCollection(Config::all());
$jsonString = $data->toResponse(\request())->getContent();

This jsonString I can use as Body of the POST-request. But it seems a bit complicated. My first attempt was to use the $data->toJson()-method. But it does not do the wrapping like it does when returning the collection as a response.

Is there maybe an easier way to build wrapped-JSON-requests?

Best regards Andi

0 likes
6 replies
krisi_gjika's avatar

The chance that your api is the exact same as some other outgoing api seems very low. If it is, what is your api even doing than?

Andi1982's avatar

@krisi_gjika It is some Middleware service from inhouse which is used to communicate with remote systems.

But even if it is own system which has to post data to some other API, it is not good idea to use resource classes to build the request json body json to do the POST then?

martinbean's avatar

it is not good idea to use resource classes to build the request json body json to do the POST then?

@Andi1982 It‘s not what they’re intended for, no.

Sounds like you want some sort of data mapper class that transfers to and from the external API representation to your internal representation.

jlrdw's avatar

@Andi1982 normally regular back end code is used with a get or post. The response is usually json or xml, sometimes csv.

What is it you are wanting to do?

Andi1982's avatar

I want to call a REST API which requires a huge json-object-structure.

For example this kind of JSON I have to POST to this API:

POST /api/v1/createOrder

{
  "distanceUnit": "example-data",
  "finOrVin": "example-data",
  "grossNetIndicator": "example-data",
  "serviceMeasures": "example-data"[
    {
      "identifier": "example-data",
      "codeword": "example-data",
      "origin": "example-data",
      "operations": "example-data"[
        {
          "operationValue": "example-data",
          "origin": "example-data",
          "description": "example-data",
          "priceGrossValue": "example-data",
          "editRestrictedComment": "example-data",
        }
      ],
      "parts": "example-data"[
        {
          "critical": "example-data",
          "part": "example-data"{
            "number": "example-data",
            "es1": "example-data",
            "es2": "example-data""
          },
          "origin": "example-data",
          "description": "example-data",
          "availabilityDate": "example-data",
          "priceGrossValue": "example-data",
          "editRestrictedComment": "example-data",
          "faultLocations": "example-data"[
            "abc",
            "def"
          ],
          "priceNetValue": "example-data",
        }
      ],
      "etag": "example-data",
    }
  ],
  "warrantyCode": "example-data",
  "offerPriceLocked": "example-data",
  "licensePlateNumber": "example-data",
  "customerConcerns": "example-data"[
    {
      "identifier": "example-data",
      "reproducible": "example-data",
      "origin": "example-data",
      "cause": "example-data",
      "type": "example-data",
      "title": "example-data",
      "operations": "example-data"[
        {
          "operationValue": "example-data",
          "origin": "example-data",
          "description": "example-data",
          "priceGrossValue": "example-data",
          "editRestrictedComment": "example-data",
        }
      ],
      "customerStatementNote": "example-data",
      "parts": "example-data"[
        {
          "critical": "example-data",
          "part": "example-data"{
            "number": "example-data",
            "es1": "example-data",
            "es2": "example-data""
          },
          "origin": "example-data",
          "description": "example-data",
          "availabilityDate": "example-data",
          "priceGrossValue": "example-data",
          "editRestrictedComment": "example-data",
          "faultLocations": "example-data"[
            "abc",
            "def"
          ],
          "priceNetValue": "example-data",
          "lastDmsPriceUpdate": "example-data",
        }
      ],
      "servicePackages": "example-data"[
        {
          "userFriendlyTextLanguage": "example-data",
          "identifier": "example-data",
          "sequenceNumber": "example-data",
          "origin": "example-data",
          "priceType": "example-data",
          "userFriendlyText": "example-data",
          "description": "example-data",
          "completed": "example-data",
          "metaPackageId": "example-data",
          "invoiceCode": "example-data",
          "editRestricted": "example-data",
          "editRestrictedComment": "example-data",
          "number": "example-data",
          "operations": "example-data"[
            {
              "operationValue": "example-data",
              "origin": "example-data",
              "description": "example-data",
              "priceGrossValue": "example-data",
              "editRestrictedComment": "example-data",
            }
          ],
          "sppsGroupPriceName": "example-data",
          "lastDmsPriceUpdate": "example-data",
          "parts": "example-data"[
            {
              "critical": "example-data",
              "part": "example-data"{
                "number": "example-data",
                "es1": "example-data",
                "es2": "example-data""
              },
              "origin": "example-data",
              "description": "example-data",
              "availabilityDate": "example-data",
              "priceGrossValue": "example-data",
              "editRestrictedComment": "example-data",
              "faultLocations": "example-data"[
                "abc",
                "def"
              ],
              "priceNetValue": "example-data",
              "lastDmsPriceUpdate": "example-data",
              "dmsOrigin": "example-data",
              "identifier": "example-data",
            }
          ],
          "etag": "example-data",
          "prices": "example-data"{
            "sppsNetValue": "example-data",
            "sppsGrossValue": "example-data",
            "dmsNetValue": "example-data",
            "dmsGrossValue": "example-data",
            "serviceManagementGrossValue": "example-data"
          },
          "dmsOrigin": "example-data",
          "refund": "example-data",
          "dmsExternalId": "example-data"
        }
      ],
      "etag": "example-data",
      "comment": "example-data",
      "damageKeyCode": "example-data",
      "damageKeyDefectTypeDescription": "example-data",
      "correction": "example-data",
      "dmsExternalId": "example-data""
    }
  ],
  "basicAndMaintenanceData": "example-data"{
    "previousServiceDate": "example-data",
    "odometerSource": "example-data",
    "odometer": "example-data"{
      "unit": "example-data",
      "value": "example-data"0
    },
    "operatingTimeValueInHours": "example-data",
    "acceptancePerformedAtVehicle": "example-data",
    "maintenanceDeleted": "example-data",
    "dsbDocuState": "example-data",
    "serviceCode": "example-data",
    "footnoteLanguage": "example-data",
    "viscosityManualEntered": "example-data",
    "startRangeInMiles": "example-data",
    "footnote": "example-data",
  },
  "automaticTransfer": "example-data",
  "certifiedServiceAdvisor": "example-data"{
    "firstName": "example-data",
    "lastName": "example-data",
    "identification": "example-data""
  },
  "currency": "example-data",
  "state": "example-data",
  "certifiedTechnician": "example-data"{
    "firstName": "example-data",
    "lastName": "example-data",
    "identification": "example-data""
  },
  "order": "example-data"{
    "acceptanceDate": "example-data",
    "costEstimateNote": "example-data",
    "orderId": "example-data",
    "courtesyMobilityNote": "example-data",
    "receptionType": "example-data",

  },
  "customer": "example-data"{
    "lastName": "example-data",
    "preferences": "example-data",
    "businessCustomerAddress": "example-data",
  }
}

To create this JSON-Structure I created a CreateOrderResource.php and in the toArray() method I grabbed the data out of my database from different kind of models and filled up the array.

But because I did not have to respond this data, I need it in a variable to POST it to https://target-api.xxx/api/v1/createOrder

For me seemed to be good idea to use the Resource functionality because it supports the model and relationship stuff. But to get the JSON out is little bit tricky.

So I just wanted to ask how others are building up structures like this without resources.

Please or to participate in this conversation.