Forum Discussion

Lewis_Beard's avatar
2 years ago

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!

  • You need to url encode the quotes if they aren’t already. So when you send it, it needs to look like this.

    /setting/admins?filter=status%3A%22active%22 

    Things like Postman auto do that for you.

  • You need to url encode the quotes if they aren’t already. So when you send it, it needs to look like this.

    /setting/admins?filter=status%3A%22active%22 

    Things like Postman auto do that for you.

  • Anonymous's avatar
    Anonymous

    Separate 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:

    resourcePath = "/setting/admins"
    queryParams = "offset=${offset.toString()}&size=1000&filter=status:\"active\""
    url = "https://${account}.logicmonitor.com/santaba/rest${resourcePath}?${queryParams}"
    println(url)

    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?

  • Anonymous's avatar
    Anonymous

    Joe’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.

  • Stuart,

    I dont think its an auth error, because without filters, I'm getting back all my users and I can loop through. And I can even add filter= onto the end with nothing there, and that works. I can even do other filters and get back the responses I expect, such as filter=id:1234 for my user id. I can even do status:11111 just to show that my string overall works, and that its just the value for status I cant get right as a string.

    But as soon as I do filter=status:"active" then I get a string error.

    Yours: {{url}}setting/admins?filter=status:"active"
    Mine:  {{url}}setting/admins?size=1000&offset=0&filter=status:"active"

    You can see below (company redacted) that its some kind of illegal character when I'm using groovy to do the http get. I admit I'm stumped.

    The script failed, elapsed time: 0 seconds - Illegal character in query at index 93: https://REDACTED.logicmonitor.com/santaba/rest/setting/admins?size=1000&offset=0&filter=status:"active"
    java.lang.IllegalArgumentException: Illegal character in query at index 93: https://REDACTED.logicmonitor.com/santaba/rest/setting/admins?size=1000&offset=0&filter=status:"active"
        at java.base/java.net.URI.create(URI.java:883)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
        
    I appreciate the help, and I know its rough with people just asking you do debug their junk. :)

    I was hoping I was doing something fundamentally wrong with the URL and etc itself, but I think mine matches yours for the filter syntax. So I'm stumped. :)

    Thanks!
     

  • Thanks, 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’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!