How to exclude empty input from Request Body in HTTP Method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2023 04:58 PM
Hello,
I have a macro that I am currently writing. I am grabbing the input, passing it to a script include via GlideAjax, and then calling a REST Message I have configured. Everything works great if all the input is filled in. If an item is missing however, then I get an error. The API we are using allows for multiple combinations of search, but will not accept empty input. How can I exclude the empty variables from my content in my POST method?
The way I currently have my POST Method configured is I use variable substitutions:
- userId
- firstName
- lastName
I build my content similarly:
[ { "key": "userId", "value": "${userId}" }, { "key": "firstName", "value": "${firstName}" }, { "key": "lastName", "value": "${lastName}" } ]
In my Script Include, I do the following:
var userId = this.getParameter('sysparm_userId'); var firstName = this.getParameter('sysparm_firstName'); var lastName = this.getParameter('sysparm_lastName'); try { var r = new sn_ws.RESTMessageV2('Get_User_Data', 'POST User Data'); r.setStringParameterNoEscape('userId', userId); if (firstName != '') { //optional in search r.setStringParameterNoEscape('firstName', firstName); } if (lastName != '') { //optional in search r.setStringParameterNoEscape('lastName', lastName); } var response = r.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); } catch (ex) { var message = ex.message; }
firstName and lastName are not mandatory, but are used to refine the search if needed.
So to prevent an error in the search, how can the content be dynamic to exclude this information from the search if no input is entered? You can also see that I do have validation for empty inputs, but this is not working.
Thanks!
- Labels:
-
Architect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2023 10:40 PM
try below:
if(!gs.nil(firstName)
{
// your code
}
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2024 02:21 AM
Hello @mballinger , did you get a solution to this?