1

I have a REST API method that consumes an array of apex objects:

    @RestResource(urlMapping='/tmstruckpostingservice/*')
global with sharing class TMSTruckPostingService {
    
    @HttpPost
    global static List<Id> postTrucks(List<WSTruckPosting> pList) {

        try {
            return createTruckPostings(pList);
        }
        catch(Exception e) {
            Logger.logAlways(e, e.getMessage());
            throw e;
        }
    }

    global class WSTruckPosting {

        public WSTruckPosting() {}

        public String   carrierCountryCode              {get;set;}
        public String   carrierName                     {get;set;}
        public String   carrierStateProvinceCode        {get;set;}
        public String   comments                        {get;set;}
        public String   contactEmail                    {get;set;}
        public String   contactName                     {get;set;}
        public String   contactPhone                    {get;set;}
        public String   dataSource                      {get;set;}
        public Date     dateAvailable                   {get;set;}
        public Integer  dayOfWeekAvailable              {get;set;}
        public Datetime datetimeCreated                 {get;set;}
        public Integer  deliveryRangeInMiles            {get;set;}
        public String   destinationCity                 {get;set;}
        public String   destinationCountryCode          {get;set;}
        public Decimal  destinationLatitude             {get;set;}
        public Decimal  destinationLongitude            {get;set;}
        public String   destinationStateProvinceCode    {get;set;}
        public String   dotNumber                       {get;set;}
        public String   equipmentDescription            {get;set;}
        public String   equipmentOptions                {get;set;}
        public Boolean  fullTruckload                   {get;set;}
        public String   mcNumber                        {get;set;}
        public Integer  minimumMiles                    {get;set;}
        public Integer  numberAvailable                 {get;set;}
        public String   originCity                      {get;set;}
        public String   originCountryCode               {get;set;}
        public Decimal  originLatitude                  {get;set;}
        public Decimal  originLongitude                 {get;set;}
        public String   originStateProvinceCode         {get;set;}
        public Integer  pickupRangeInMiles              {get;set;}
        public Id       truckPostingId                  {get;set;}
    }

However when I try to test this by passing the following JSON in Postman I get the error which follows:

BODY:

{ "data" : [{
"carrierCountryCode" : "US",
"carrierName" : "Test Carrier",
"carrierStateProvinceCode" : "IL",
"comments" : "Hello Newman!",
"contactEmail" : "support@revenova.com",
"contactName" : "Dave Test",
"contactPhone" : "312-333-8888",
"dataSource" : "Tobor",
"dateAvailable" : "2018-04-19",
"dayOfWeekAvailable" : 1,
"datetimeCreated" : "2018-04-19T14:00:58.000Z",
"deliveryRangeInMiles" : 100,
"destinationCity" : "Chicago",
"destinationCountryCode" : "US",
"destinationLatitude" : 42.00000,
"destinationLongitude" : -87.0000,
"destinationStateProvinceCode" : "IL",
"dotNumber" : "1234567",
"equipmentDescription" : "53 Foot Dry Van",
"equipmentOptions" : "Tarps",
"fullTruckload" : true,
"mcNumber" : "MC92929",
"minimumMiles" : 100,
"numberAvailable" : 2,
"originCity" : "San Jose",
"originCountryCode" : "US",
"originLatitude" : 41.0000,
"originLongitude" : -88.000000,
"originStateProvinceCode" : "CA",
"pickupRangeInMiles" : 100,
"truckPostingId" : null 
}]}

ERROR:

    [
    {
        "message": "Unexpected parameter encountered during deserialization: data at [line:1, column:13]",
        "errorCode": "JSON_PARSER_ERROR"
    }
]

I have several other similar REST services that work just fine with the exact same form EXCEPT they are consuming individual objects, not an array of objects. So it is not the global/public usage I employ, which works fine for my other services,

Any thoughts on why this does not work? The debug log shows no errors. The header fields are parsed and then the method returns according to the logs.

Thanks!

1

Stupid human trick on my part. When I replace "data" with "pList" it works.

Basically, you need to name your packet the same as the method parameter.

Note this is only relevant for my use case - Postman or SOAPUI invocation.

1

The REST API simply receives a list of WSTruckPosting instances, as the named parameter, so the payload should be simply a JSON object with a "pList" property that is an array of JSON objects with equivalent properties:

{
  "pList": [
  {
    "carrierCountryCode" : "US",
    "carrierName" : "Test Carrier",
    "carrierStateProvinceCode" : "IL",
    "comments" : "Hello Newman!",
    "contactEmail" : "support@revenova.com",
    "contactName" : "Dave Test",
    "contactPhone" : "312-333-8888",
    "dataSource" : "Tobor",
    "dateAvailable" : "2018-04-19",
    "dayOfWeekAvailable" : 1,
    "datetimeCreated" : "2018-04-19T14:00:58.000Z",
    "deliveryRangeInMiles" : 100,
    "destinationCity" : "Chicago",
    "destinationCountryCode" : "US",
    "destinationLatitude" : 42.00000,
    "destinationLongitude" : -87.0000,
    "destinationStateProvinceCode" : "IL",
    "dotNumber" : "1234567",
    "equipmentDescription" : "53 Foot Dry Van",
    "equipmentOptions" : "Tarps",
    "fullTruckload" : true,
    "mcNumber" : "MC92929",
    "minimumMiles" : 100,
    "numberAvailable" : 2,
    "originCity" : "San Jose",
    "originCountryCode" : "US",
    "originLatitude" : 41.0000,
    "originLongitude" : -88.000000,
    "originStateProvinceCode" : "CA",
    "pickupRangeInMiles" : 100
  },
  {
    ...
  }]
}

NB: You don't have to pass null properties (though you can).

3
  • Thanks for the response, Phil. No luck with that approach: "message": "JSON request body must be an object at [line:1, column:2]" – Dave C Mar 29 at 19:11
  • Dave, sorry about that, could have sworn it worked that way with one param. I see you answered, I will adjust my answer accordingly. – Phil W Mar 29 at 21:59
  • Thanks again, Phil. – Dave C Mar 30 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.