Skip to main content

drupal_add_http_header() replacement in Drupal 8

Sometimes, while custom coding, it require headers like content-type to be added to the responses by using drupal_add_http_header() in Drupal 7. But it deprecated in 8. Drupal API talking about Symfony object Response to use in order to its replacement.

For example, in Drupal 7

drupal_add_http_header('Content-Type', 'text/csv; utf-8');

The equivalent / replacement , in Drupal 8

use Symfony\Component\HttpFoundation\Response;
$response = new Response();
$response->headers->set('Content-Type', 'text/csv; utf-8');