Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

HTTP post method optional parameter in json content

Felix20
Mega Guru

Hi all,

I'm working on integration of Microsoft Graph API into my application and facing an issue with optional parameter in the json body content.

How can I filter optional parameter of that json content of my HTTP method defined in my REST Message, if it has not been set in my workflow?

To be more precisely:

I have a RestMessage "Graph API User" and a corresponding HTTP Method "updateUser PATCH" which has a predefined content of the following:

{
"preferredLanguage": "${preferredLanguage}",
"givenName":"${givenName}",
"surname":"${surname}"
}

 

I want to use that REst message + http method in a script but i just want to set preferredLanguage as a parameter. So I just pass that one as a settingparameter.

But when i fire that rest message, i get an error, that the json format is wrong, because givenName and surname will also be part of the message but have no value.

How can i prevent them to be part of the requestBody when i use it in a script?

 

 

Many regards

 

1 ACCEPTED SOLUTION

What I've done is a create a script include with all the functions needed that I can call from a BR, workflow or whatever triggers the message. That way you have one script included where you can maintain call all the rest messages. That should help you take care of the "," part as well.

It's not ideal but if MS would add a new variable that is needed for the payload you would still need to add it to the rest message and then add it as a parameter to all places where you use that rest message. So building the payload in a script included and use that might actually be the easiest. Apart from the third party not changing the API ofc 🙂

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Felix,

Does the target system require those values for processing the API endpoint?

If yes then you need to pass those values. If they don't require you can set it as empty if you don't have any precise values for those.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, not exactly. The API requieres at least one of those parameters to update the user object in azure ad. So I could send all three or just a subset. Sending empty values is not allowed because that would indicate a deletion of the current information of the user, for example

{ "preferredLanguage": "de-DE",
   "givenName":"Felix",
   "surname":""
}

Then azure will remove my surname. In that case I would only like to send:

{ "preferredLanguage": "de-DE",
   "givenName":"Felix"
}

I dont want to set the whole response object in my script, but in my rest message object for dokumentation.

Long story short: How to remove optional ("unset") parameters of a predefined content of rest message / http method?
In this case, how to remove ',"surname":"${surname}"' completely?

 

Best regards Felix

simonbergstedt
Tera Guru

Is the payload you typed here the actual payload? Because then it's not valid since you lack a " at the end of surname which makes in invalid json try it on https://jsonlint.com/

{
"preferredLanguage": "${preferredLanguage}",
"givenName":"${givenName}",
"surname":"${surname}
}

Other then that if you don't want to send them at all couldn't you just remove them from the HTTP Request in the Rest Message-configuration? If you want to send them in some cases then you could either make the whole the name and the value it's own attribute and just populate them when you need them. Or you could set up different Methods

Hi simon, you are totally right that a " is missing at the end.

But finally thats not the problem I'm facing.
Please have a look at my reply to ankurs post.

Best regards, Felix