Managing Optional Parameters in API Requests

Hi,

I’m working on building an application that utilizes the UPS API. I previously had a question regarding storing a response with a status code of 400. Now, I have another question: in the API request body, you can optionally add extra shipment service options. For instance, when you add “UPScarbonneutralIndicator” to your request, the costs are immediately calculated, regardless of whether you input an empty value. The issue here is that if someone doesn’t want to use this feature, this item should not be included in the request body. On the other hand, if someone does want to use this feature, it must be included in the request. However, creating separate API calls for each service option where some rules are included or excluded is cumbersome. I’m curious if there’s a solution for this?

Thanks in advance,
Melissa

Hi @melissa,

Yes, there is a simple way to do this. If you make sure that the value in your flow part is Empty when you don’t want to include it, you can utilize a feature that we have.

When you include a value in the json message, it will ask What should happen when this value is empty?. One of the options is to not include the value:

image

This is only available for values, not for the object (in your case ShipmentServiceOptions. From your message I deduce that if you send ShipmentServiceOptions without the nested UPScarbonneutralIndicator, that this will count as it not being supplied. So this solution should work in your case.

1 Like

Hi Jesse,

I’ve attempted this by creating a calculation action with an empty value and sending it through the API. However, the costs were still being included. What I’ve done now is added a random data object to the input of the action and included it in the request. When I provide this data object input in the user flow where the action is located, costs are incurred; if I don’t include it, costs are not calculated. So, it seems to be working in this manner.

1 Like

Glad to hear it works. I will ask the team to double check this case with a calculation, since it should just work when the value is empty, that it isn’t included.

1 Like

I’ve attempted this by creating a calculation action with an empty value and sending it through the API.

Hi @melissa,

We looked into this issue and beware: we’re going to get into some nitty gritty details with this answer :slight_smile:

What you selected in a calculation, is an empty text (TextAny text and then you left the field empty). So this is a text, without any characters in it. And while this may indeed seem like the same as when any value is empty, it is not for a computer.

For a computer there is another value that represents Empty, which is called null. So the computer makes a distinction between an empty text, and a completely absent value (null).

That’s the reason that you still got that value sent in the message, because Triggre represents the empty text as Empty. There is a lot of logic inside the Triggre platform to make handling empty values as easy as it is, but this specific case needs some improvement I guess :slight_smile:

We are thinking about how to better represent this specific case, thanks for bringing it to our attention!

1 Like

Hi Jesse,

Thank you for the detailed explanation! I look forward to any updates and appreciate your attention to this matter.

Melissa

1 Like