string filters in the API (groovy)
In the past I have not had much need of filters in API, and on the rare occasion I have, it has been numeric filters. But for some reason I cannot get string filters to work at all without throwing errors, either 400 responses, or else unexpected character errors.
I can do this all day and get results:
queryParams = '?offset=' + offset.toString() + '&size=1000&filter=id:1256';
But as soon as I try to make my filter a string filter, I get errors, and I’ve seen both of the following used in various LM docs online, as if they would work, but I just get unexpected string errors:
queryParams = '?offset=' + offset.toString() + '&size=1000&filter=status:active';
queryParams = '?offset=' + offset.toString() + '&size=1000&filter=status:”active”';
Basically, I’m pretty sure I’m just not passing in the string value I want for status in a correct way. I’ve literally seen LM documentation of status:active and I’ve seen other examples when they do name~”whatever” but both of these throw errors. I can even test for status being a number (which obviously give no results), and with no filter, I get back everything (I’m doing setting/admins).
And I’ve tried every permutation of quotes in double quotes or backslashed quotes or double and triple quotes and double quotes. Because it something NEWBIE-ish I’m doing wrong.
When building a url for the API in groovy, is there some specific way to quote up a string value on a filter?
//build the request URL
resourcePath = "/setting/admins";
//queryParams = "?size=1000&offset=" + offset.toString();
//queryParams = '?offset=' + offset.toString() + '&size=1000&filter=id:1256';
//queryParams = '?offset=' + offset.toString() + '&size=1000&filter=status:1256';
queryParams = '?offset=' + offset.toString() + '&size=1000&filter=status:active';
queryParams = '?offset=' + offset.toString() + '&size=1000&filter=status:”active”';
url = "https://" + account +".logicmonitor.com/santaba/rest" + resourcePath + queryParams;
The first 3 commented items work fine (tho the 3rd one has no results obviously because 1256 isnt a valid status) but as soon as I try status:active or status:”active” its game over.
What newbie thing am I doing wrong?
Thanks in advance. Cheers!
whitwa1
·3 years agoCould someone please post an example of the expected encoding when using advanced filters (customProperties in my case). I am using C# btw.
See REST API Advanced Filters | LogicMonitor
Regards,
Andrew
Lewis_Beard
OP3 years agoJoe’s answer should be marked as the right answer. I can’t believe i forgot about that, but i probably built the encoder into my query param handler function. FWIW: if you encode just the value portion of each query parameter, then join together with the key, then join each key value pair together, it should work cleanly. I get it if you don’t want to do that.
Sounds like a good idea, I’ll give it a shot.
Thanks Stuart, and thanks Joe!
LM User
·3 years agoJoe’s answer should be marked as the right answer. I can’t believe i forgot about that, but i probably built the encoder into my query param handler function. FWIW: if you encode just the value portion of each query parameter, then join together with the key, then join each key value pair together, it should work cleanly. I get it if you don’t want to do that.
Lewis_Beard
OP3 years agoThanks, that worked out for me. I had to hard code the %3A and %22, but it worked perfectly. When I tried to use some html escape package, it also converted the & to the html notation, which made the filter NOT work. But if I leave the ampersand as-is and manually do the rest, it works brilliantly.
Thanks to both of you!!!
Joe Williams
·3 years agoYou need to url encode the quotes if they aren’t already. So when you send it, it needs to look like this.
Things like Postman auto do that for you.
LM User
·3 years agoSeparate your problems by getting it to work in postman first. Then you’ll know if the problem is syntax in groovy or syntax in the api call itself.
{{url}}/setting/admins?filter=status:"active"
This ^^ works for me in postman.
So, you just want your url string to result in that same thing:
I took this url and popped it into postman and it worked just fine.
Are you getting an authentication error? Is it possible you’re not calculating your signature properly?