7

REST API works, but I cannot pass json parameter to it, getting "Unable to decode supplied JSON".

I am running CiviCRM 4.6.8 on Wordpress.

I have properly identified SITE KEY and API KEY, and I can run REST query with an empty json dictionary as parameter:

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&json={}

which gives me 25 contact records in a json data structure.

But when I try to add a json parameter (copied from API Explorer):

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&json={%22sequential%22:1,%22email%22:%22marcin@akcjademokracja.pl%22}

..I get the following error:

{"is_error":1,"0":"error_message","1":"Unable to decode supplied JSON."}

What am I doing wrong?

  • Have you tried passing double-quotes directly (rather than the %22 encoding)? – DaveD - BackOffice Thinking Sep 21 '15 at 21:51
  • yes, with the same result – movonw Sep 21 '15 at 22:19
  • I never had any luck getting json parameters to work in GET requests. As Xavier said it works fine in POST, though. – Andrew West Sep 22 '15 at 10:32
  • I get the same issue with the json={"last_name":"somename"} way of phrasing the query. Now, I am, admittedly, brand new to Civi So all input is very welcome :) I will be creating services that query and hopefully update Civi records and the json= method looks like it would allow for a more flexible interface, in that I can easily construct a json query and insert it. It will be more fiddly to maintain separate arguments or even interfaces to represent the different queries I may want to call. SO, does the API call with json={some json query here}, not work at all? I took the examples from the – CiviStackStu Nov 8 '16 at 16:46
  • It doesn't work for me either... – Francis Poisson Apr 6 '17 at 1:01
7

I would recommend you to use post instead of get to communicate with the rest api, both because it would allow you to avoid having the keys "in clear" in the get url (and therefore written in the log an so on), but also because you don't have to deal with url encoding of the parameters.

btw, you don't have to put the param in the json most of the time, so you can directly call

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&sequential=1&email=marcin@akcja.pl&json=1

  • I tried to do a POST. When I sent the json parameter I had the same error as in the original question. When I sent parameters separately, I got a proper XML response. Because I wanted a JSON date returned, I have also added a json={} parameter which triggers returning data as JSON. Still, there seems to be a bug in CiviCRM regarding JSON parsing. – movonw Sep 22 '15 at 13:14
  • if you want json, put json=1 – Xavier Sep 22 '15 at 13:34
  • you obviously don't have to url encode the json param, just "normal" json, should work fine. try decoding the json on the client, see if it works. The decoding message is fairly early in the code, it really just mean that, the formatting is wrong somewhere ;) – Xavier Sep 22 '15 at 13:37
0

Strangely, le API explorer says to call the REST API with request attributes in the json parameter, like this:

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&json={%22sequential%22:1,%22email%22:%22marcin@akcjademokracja.pl%22}

But, as explained in the documentation, the json parameters just defines if the result should be json or xml. it just takes values 0 or 1.

All parameters set in the json example have to be passed in the query string.

In your case, your syntax should be:

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&sequential=1&email=marcin@akcjademokracja.pl&json=1

which will return a json response

or:

https://foo.com/wp-content/plugins/civicrm/civicrm/extern/rest.php?entity=Contact&action=get&api_key=secret1&key=secret2&sequential=1&email=marcin@akcjademokracja.pl

for an XML response.

By the way, for security reasons, parameters should be passed in POST, especially api_key and key to prevent them for being store in logs.

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.