When I call this:
return services_error(t('The username already exists.'), 406);

In a services callback function, and I ask for a JSON result (Accept: application/json), the webservice will output invalid JSON, in fact it will just output

"The username already exists."

Which is invalid JSON - this causes some of the web service clients to crash, since services are still telling that it has Content-type: application/json in the header.

Comments

esbenvb’s picture

Here is a patch for the current 7.x-3.x branch.

It might screw up other things in some installations, but fixes the issue for me.

wwedding’s picture

Title: Invalid JSON returned by services_error() » Invalid JSON returned by services_error() or resources returning non-array/non-object values
StatusFileSize
new735 bytes

This is a problem with pretty much any data that is being expressed that isn't in an array or object form already; strings, booleans, integers, etc. json_encode() doesn't really do what it seems like it should for these.

My fix was to just wrap the content in an array inside the REST server's render_json() function...

  private function render_json($data, $jsonp = FALSE) {
    //json_encode doesn't produce valid json with bools, strings, or other simple data
    if(!is_array($data) && !is_object($data)) {
      $data = array($data);
    }
    $json = str_replace('\\/', '/', json_encode($data));
    if ($jsonp && isset($_GET['callback'])) {
      return sprintf('%s(%s);', $_GET['callback'], $json);
    }
    return $json;
  }
wwedding’s picture

Status: Active » Needs review

I guess my fix is specific to the built in REST server and the original poster's patch might be a more general fix? Maybe this isn't a problem with other server types.

@esbenvb are you using REST?

kylebrowning’s picture

I like the patch in #1 better, but #2 raises a good point!

Anyone else want to chime in?

ygerasimov’s picture

I think we should proceed with second patch as it breaks our APIs less that first one.

marcingy’s picture

Status: Needs review » Needs work

Second one makes me happier but the code has a few style issues

Missing space after // and line is over characters long.

+    //json_encode doesn't produce valid json with bools, strings, or other simple data

Missing space after if

+    if(!is_array($data) && !is_object($data)) {
wwedding’s picture

Status: Needs work » Needs review
StatusFileSize
new730 bytes

Sorry about that, forgot to run the patch through code sniffer before submitting.

Shortened the comment, added a space, added a full stop, fixed the spacing around the conditional.

ygerasimov’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

kylebrowning’s picture

Status: Reviewed & tested by the community » Fixed
kylebrowning’s picture

I hope this doesnt break anyones clients :(

wwedding’s picture

It might, at least temporarily. But only because those clients had to be coded to handle improper non-JSON responses when they expected JSON responses in the first place, right?

kylebrowning’s picture

Ill just make it apparent in the release notes that its a potential breaker.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.