7

I have a custom module which are basically APIs, hence require headers like content-type to be added to the responses. I was using drupal_add_http_header() in Drupal 7, but it seems to be deprecated in 8. Is there any replacement?

I did read about it on the Drupal website talking about Symfony object Response, but I am unable to use that either.

  • As you mentioned, the Symfony Response object is D8's replacement for using drupal_add_http_header. You should address why you can't use it. – Shawn Conn Apr 22 '16 at 6:55
  • Since it requires me to pass a response object to the function already. Can I achieve the same by using php's header function ? – Hemant Kumar Goyal Apr 22 '16 at 6:58
  • Welcome to Drupal Answers! You should show the code you are using, and give more details. For example, from your description is not clear if you are using a controller or what, and your comment is a little confusing. Why would pass a response object to a function? It is enough you return it to Drupal, but without seeing any code, it is not possible to tell you the code you should use. – kiamlaluno Apr 22 '16 at 7:10
  • I found a discussion thread from www.drupal.org may be this can be of little help. 1. drupal.org/node/2307723. 2. drupal.org/node/2467759. – Shreya Shetty Apr 22 '16 at 8:22
  • You can use $build['#attached']['http_header'] to add a custom header. – kenorb Aug 27 '17 at 21:21
10

If you want to modify the headers of a response, you need to use an EventSubscriber. Symphony doesn't have a hook system, but instead uses an event/emitter system. Since it's Symphony driving the request/response cycle, you basically needs to integrate with Symphony to do this.

You could take a look at \Drupal\Core\EventSubscriber\FinishResponseSubscriber to see how this is done, the gist of it is this:

/**
 * Add custom headers.
 */
class HeaderResponseSubscriber implements EventSubscriberInterface {
  public function onRespond(FilterResponseEvent $event) {
    $response = $event->getResponse();
    $response->headers->set('Some-Header', 'some value'); 
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array('onRespond');
    return $events;
  }
}

Note in the above code all use statements have been excluded, also you need to register your class as a event_subscriber service, this is done in your module's module_name.services.yml file like this:

services:
  name_of_service:
    class: Drupal\Full\Namespaced\Path\To\Class
    tags:
      - { name: event_subscriber }

You can add arguments (other services) that your class will depend on in the services file like normally.

| improve this answer | |
  • 6
    Good answer, but worth mentioning that you only need this if you need to alter/extend responses created by someone else. If you return the response yourself, just call those methods directly. And if you want to do it as part of something that returns a render array (a block, a field, whatever), you can use #attached: $build['#attached']['http_header'][] = [['Content-Type', 'application/rss+xml; charset=utf-8']]; (example is from \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments) – Berdir Apr 22 '16 at 22:54
  • 2
    Another example can be found at: chapterthree.com/blog/how-to-register-event-subscriber-drupal8 – gerzenstl Nov 1 '16 at 17:49

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