Carrier RestAPI - set response as a variable

Hi, I have created successful requests through Postman to our Carrier to create a consignment but im having issue converting to Custom Code in a BPM through Epicor.

I currently have the code below which works and retrieves a Token to use in the next Rest call.
image

I am unsure of how to add a field from the response as a variable for the next rest call.

If your receive a JSON resoonse you can use Newtonsoft to deserialize it.

This might also be helpful.

@stoyanlevakov
Thank you for pointing me in this direction, I’m getting somewhere.
I have tried the following but instead of using the ‘key’ i retrieved from the rest call it just shows whatever is typed between the 2 quote marks on the ‘string json’ line

Ideally i would only need to extract the value rather than the field name.
image

I am definitely getting somewhere now though! :slight_smile:

string json should be equal to your response content. Something like that:

string json = response.content;

yeh, ive tried that. I still get Key = “”
the response I get is below and in green in the field i need to pick out.

image

Ahh okay, you have to change your definition to match your response:

var definition = new { 
    accessToken = new {
        key = "",
        expiry = ""
    },
    refreshToken = new {
        key = "",
        expiry = ""
    }
};

string json = response.Content;
var token = JsonConvert.DeserializeAnonymousType(json, definition);

after that you shoulb be able to access it like

var key = token.accessToken.Key;

or something similar

@stoyanlevakov
That is magic.
Thank you so much, the Key is now being displayed so i can pass that back up to the carrier to create a consignment.

Also thank you for the short explanation on the definition aspect of the code.

1 Like

@stoyanlevakov Apologies for opening this back up.
I have tried searching for methods on how to serialize a json list but I’m coming up with nothing.
for example,

image

The below is my code (i know the trackingNumbers section is completely incorrect)

image

The SERVICE deserialization works perfectly but I’m failing on the bottom trackingNumbers section.
Any help here would be greatly appreciated.

1 Like

Hi,

In this case do you want to deserialize the data you receive in the response, or send some data to the API?

The tracking numbers is an array:

trackingNumbers = new[] {"1","23","3"}

@stoyanlevakov
Thank you, i think that array definition has worked :slight_smile:
Speaking of sending some data, how would i send a field or variable to the api.

I currently have the below and i would like to submit the tracking number field from Shiphead but im not convinced this will work.

image

I do it like that:

var bodyrl = new {
      trackingNumber = ShipHead.TrackingNumber,
      printWholeConsignment = true
};

requestrl.AddJsonBody(bodyrl); // Anonymous type object is converted to Json body

I don’t think serialization is needed when using RestSharp

perfect :slight_smile:

I now have the Label ZPL code stored as a variable.
I just need to figure out how to print it to a WorkStation Printer now without any interaction.

@stoyanlevakov

Sorry again, I’m having some real trouble sending address data to the API now.
The code on the right works fine but i cant dynamically change the address details.
The code on the left throws Json Deserialization errors and i cannot find why it is incorrect.
Any ideas?

Serialize the code on the left before sending it s hould work:

var bodyccSer = JsonConvert.SerializeObject(bodycc,Formatting.Indented):

Still throws the same error.
I couldn’t put the serialize code before setting bodycc as it had not been declared yet.

You serialized bodycc into bodyccser but still sent bodycc.

1 Like

Pass the bodyccSer insead of bodycc

1 Like

Thanks but I’m still getting the same error.

Event Viewer error from server is below.

Is this the whole code in this BPM ?