Carrier RestAPI - set response as a variable

@stoyanlevakov

The whole code is below

foreach (var ShipHead in (from ShipHeadRow in Db.ShipHead.With(LockHint.UpdLock)
where ShipHeadRow.Company == Session.CompanyID
&& ShipHeadRow.PackNum == ps
select ShipHeadRow))
{
var clientcc = new RestClient("https://RestAPI_Enpoint);
clientcc.Timeout = -1;
var requestcc = new RestRequest(Method.POST);
requestcc.AddHeader(“Token”, ShipHead.CarrierToken_c);
requestcc.AddHeader(“Content-Type”, “application/json”);
var bodycc = new {

              accountCode = ********,
              despatchDate = "2022-06-20",
  DeliveryAddress = new {
              company = comp,
              address1 = addr1,
              town = city,
              postcode = zip,
              countryCode = "GB"
                    },
              serviceCode = "NS",
  items = new {
              quantity = 2,
              packageTypeCode = "BX",
              weight = 1
            }     

};
var bodyccSer = JsonConvert.SerializeObject(bodycc,Formatting.Indented);
requestcc.AddJsonBody(bodyccSer);

requestcc.AddParameter(“application/json”, bodyccSer, ParameterType.RequestBody);
IRestResponse responsecc = clientcc.Execute(requestcc);
Console.WriteLine(responsecc.Content);

var definitioncc = new {
service = new {
serviceCode = “”,
name = “”,
premium = false,
saturday = false,
sunday = false,
air = false,
timed = false,
slaTime = “”,
deliveryMethod = “”,
signatureRequired = true,
neighbourSignature = true,
allowedCover = true,
maximumCover = 250,
standard = false,
supplement = false,
serviceLevel = “”,
days = 1,
b2BDeclarationType = “”,
b2BdeclarationType = “”,
serviceType = 1
},
trackingNumbers = new [] {“1”,“2”,“3”}

};

string jsonCreateCon = responsecc.Content;
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var tracking = JsonConvert.DeserializeAnonymousType(jsonCreateCon, definitioncc);
Console.WriteLine(tracking);

var track = tracking.trackingNumbers.First();

{ShipHead.TrackingNumber = track;}

}

The problem comes from the deserialization at the bottom. I think you definition doesnt match the response JSON.

Can you send me an example response?

an example response would be.

{

"service": {

    "serviceCode": "NS",

    "name": "Secure Flex",

    "premium": false,

    "saturday": false,

    "sunday": false,

    "air": false,

    "timed": false,

    "slaTime": null,

    "sla": 2400,

    "deliveryMethod": "Signature",

    "signatureRequired": true,

    "neighbourSignature": true,

    "allowedCover": true,

    "maximumCover": 250,

    "standard": false,

    "supplement": false,

    "serviceLevel": "Next day",

    "days": 1,

    "b2BDeclarationType": "NotRequired",

    "b2CDeclarationType": "NotRequired",

    "serviceType": 1

},

"trackingNumbers": [

    "7000225292",

    "7000225293",

    "7000225294"

]

}

Hmm, with this response works. But I’m still pretty sure that the error comes from the deserialization.
Can you show the response before deserializing it? Also might be a good idea to throw some try-catch blocks in the code so you can pinpoint the problem.

The deserialization works fine if i use a different request though?

The code on the right can be deserialized and gets me the response which could mean that changing the request isn’t getting a response which would possibly give the same error?

I am unsure how to put catch blocks in

@stoyanlevakov
I have found where the issue lies its the Items part of the request.
It should be a list not an object. The deserialization error was coming from the third party API.
Submitting a list seems to be limiting as the only way i can get an OK is to do the following but i would like to submit a variable instead of a static number in the Quantity.

image

create array of objects:

items = new[] { new {
        quantity = 0,
        packageTypeCode = "",
        weight = 0
     }
}

if you have multiple items:

items = new object[]{}.ToList()

then add multiple items to the array of objects

foreach(var item in items){
     bodycc.items.Add(new {
         quantity = item.quantity ,
         packageTypeCode = item.Code,
         weight = item.Weight
      });
}

Thank you, I know have a working connection to the third party API.
I don’t suppose you know how to get the ZPL code to print out to a shared printer?