1

I need to set my Content-Type header to "application/json; charset=utf-8" in Services responses.

I have tried drupal_add_http_header() in my callback, in _services_request_postprocess_alter() and in a custom ServicesFormatterInterface:

class ServicesJSON_UTF8Formatter implements ServicesFormatterInterface {
  public function render($data) {
    // json_encode doesn't give valid json with data that isn't an array/object.
    if (is_scalar($data)) {
      $data = array($data);
    }

    drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');

    return json_encode($data, JSON_UNESCAPED_UNICODE);
  }
}

Sometimes the header gets set, and sometimes it comes back just "application/json" without my addition. Where can I set this to make certain my header makes it into the response?

  • I could be insanely wrong on this, but I thought the caller is in charge of the Content-Type response format. I didn't think the Server (especially Drupal Services) was in charge of that in any way. – tyler.frankenstein Jan 21 '15 at 0:41
1

Try this ( at the end of Your service callback function ):

drupal_add_http_header('Content-type','application/json; charset=UTF-8');

echo drupal_json_encode($response);

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.