1

I am trying to do a simple GET request using a LIKE operator with a wildcard. It's easy to set up the explorer and works as expected. I'm using a demo civi environment using the example data included on install. There are two Heidis in the database.

The rest URL generated in the explorer is:
https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php ?entity=Contact &action=get&api_key=userkey&key=sitekey&json={"sequential":1,"first_name":{"LIKE":"Heid%"}}

As is, I know that the URL will generate the error: Unable to decode supplied JSON.

I have reformatted it to:
https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?key=sitekey&api_key=userkey&entity=Contact&sequential=1&json=1&action=get&first_name=Heidi

That works as expected using the exact match on Heidi. I tried using the LIKE operator using array syntax:
https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?key=sitekey&api_key=userkey&entity=Contact&sequential=1&json=1&action=get&first_name[like]=Heidi

I get the expected response, but this uses the exact value and no wildcard characters.

I've tried three different options for wildcards:

  1. Like array no wildcard:
    https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?key=sitekey&api_key=userkey&entity=Contact&sequential=1&json=1&action=get&first_name[like]=Heid
  2. Like array with wildcard:
    https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?key=sitekey&api_key=userkey&entity=Contact&sequential=1&json=1&action=get&first_name[like]=Heid%
  3. Like array with encoded wildcard:
    https://civi.site.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?key=sitekey&api_key=userkey&entity=Contact&sequential=1&json=1&action=get&first_name[like]=Heid%25

None of the three wildcard attempts produce an error, but they return 0 results. I've looked through the docs and have done multiple Google searches but I can't find a workable solution. Any help would be greatly appreciated.

2

the correct syntax is the one at the top:

"first_name": {"LIKE":"hei%"}

to work around the encoding issues (that might be tricky indeed), don't try to put all the params in the url (as http get) but use a http post (the api works with both)

3
  • Each of the top three works, it's really just the wildcard character that I'm struggling with. – DaveBagler Apr 12 at 18:17
  • use http post instead of get, solves the dealing with escaping of % and json – Xavier Apr 12 at 19:05
  • Yeah, I'll have to do it as a multipart/form-data POST. – DaveBagler May 20 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.