Kaseya Community

making filtered requests to the API

This question is answered

Hi All

run it a bit of a weird issue I have into been able to figure out.

I'm running a filtered get request against the API to return info for a given org.

My code I know is working as if I sue the same code to run the request minus the filter it returns all the orgs to me.

The filtered uri I'm using is as follows

 https://soso.co.za/api/v1.0/system/orgs?$filter=OrgRef eq 'abc52'

I have some code that generates the uri based on a variable, it outputs the above which to me looks correct.

However, when I run the GET request all that is returned is nothing or if I var_dump the output I get  string(0) "" .

I'm a little confused as to why it doesn't bring me back the info on the abc52 org as the org does exist and shows up in the unfiltered GET request.

Anybody have an idea what could be going wrong? 

Regards,

Gareth

 

Verified Answer
  • Hi Kirk

    Thanks for the reply.  I managed to figure this out this morning :) simply URL encoding the parameters was not enough for it to work with cURL. I needed to use the http_build_query function to build the URI.

    This is how I got it working with PHP and cURL :

    $host = "vsa.co.za/.../orgs";

                   $search = array('filter'=>"OrgRef eq 'abc123'");

                   $vsa = $host .="?"."\$".http_build_query($search);

    The above outputs the URI like so :

    vsa.co.za/.../orgs$filter=OrgRef+eq+%27abc123%27

    After setting it up like this my query to the API was filtered and it returned the expected result.

    Please thank your colleague for me as well, the help was much appreciated!

    Regards,

    Gareth

All Replies
  • To add to the above :

    I have tried urlencoding the parameters after the ? and then combining that to the url like so :

    $host = "soso.co.za/.../orgs

    $search = urlencode("\$filter=OrgRef eq 'abc52'");

    $vsa = $host.$search;

    This is in PHP so the \ is there to escape the $.

    The URI string outputs as :

    soso.co.za/.../orgs

    However this just seems to return all orgs as if I was just querying :

    soso.co.za/.../orgs

  • Hi @ghansford

    From a colleague not familiar with PHP, and not yet a member here... they recreated your issue in Powershell. To fix it there, she had to add ` between the ? and $ to insert the $ instead of parsing a variable. Without that it ran as if she didn’t apply a filter. With that it gave her the right org for the org ref.

    This:

    vsa.com/.../orgs`$filter=OrgRef eq 'abc123'

    Instead of:

    vsa.com/.../orgs$filter=OrgRef eq 'abc123'

    She doesn't have experience with php but says it looks like you were heading the same route.

    She asked;

    Did you try having the escape character in the original url? Or in your addition, make sure the $host still has the ? at the end when you append the filter?

    Hope this helps!

  • Hi Kirk

    Thanks for the reply.  I managed to figure this out this morning :) simply URL encoding the parameters was not enough for it to work with cURL. I needed to use the http_build_query function to build the URI.

    This is how I got it working with PHP and cURL :

    $host = "vsa.co.za/.../orgs";

                   $search = array('filter'=>"OrgRef eq 'abc123'");

                   $vsa = $host .="?"."\$".http_build_query($search);

    The above outputs the URI like so :

    vsa.co.za/.../orgs$filter=OrgRef+eq+%27abc123%27

    After setting it up like this my query to the API was filtered and it returned the expected result.

    Please thank your colleague for me as well, the help was much appreciated!

    Regards,

    Gareth