I have data in custom response headers that I need to read. I have looked in the Drupal API guide and there is a reference to Response::$headers, but I'm not sure how to exactly use this to read header data.

Edit - to further elaborate what I am trying to accomplish:

I have a form in a custom module that redirects some data in the headers to another page. Here is the code for that:

$response = new RedirectResponse('/path/destination', 302, array(
            "username" => "value"

The redirect is made successfully and the values that I'm pushing to the headers (illustrated by "username" above) are successfully transferred onto the response headers - I can see that with the browser inspector.

What I am now trying to do is read the values of those custom headers, i.e. "username", as a means of pre-populating the username field on my destination page (/path/destination).

As suggested in the answers, I have also done the following:

$response = new ResponseHeaderBag();

I also tried:

$response = new ResponseHeaderBag();
$response->get("username", $default = null, $first = true);

Even though I have both these inclusions on the top of my file:

use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

I get the following error:

Fatal error: Call to undefined method Drupal\module_name\Form\MyClass::all() in ../vendor/symfony/http-foundation/HeaderBag.php on line 113

2 Answers 2


Response::$headers is a public property, which means you can access it with $response->headers, where contains an object of the ResponseHeaderBag class or some inherited class of it.

Essentially, it means you can access the headers using either ResponseHeaderBag::all or ResponseHeaderBag::get($key).

  • Sorry, I am very new to this, how would $response->headers contain ResponseHeaderBag? I also tried to use $response, but I got a syntax error that the variable hasn't been defined.
    – PHPDev
    Apr 9, 2018 at 6:40
  • Have you made an request that should give you an response?, or are you actually referring to the current processed request?
    – johndevman
    Apr 9, 2018 at 6:57
  • The current processed request. I can see the response headers in my browser inspector, I just need to populate a form by fetching those headers.
    – PHPDev
    Apr 9, 2018 at 7:06
  • That doesn't make sense. By the time the headers are available to be read, the whole response has been built. you can't create new forms at that point. What exactly are you trying to do?
    – Berdir
    Apr 9, 2018 at 7:09
  • @Berdir I have updated my question with an elaboration of my problem.
    – PHPDev
    Apr 9, 2018 at 15:25

Headers don't work like that. First, you can't just invent arbitrary headers (or if you do, you should prefix them with X-), but even then, headers just don't work like that.

A header is just metadata about your (the server's) response. The browser doesn't do anything with it unless it is a specific header the browser knows about. It does not automatically send that header back to you.

Instead, you need to use any of the following, depending on the use case:

  • A query argument, so that your path is some/path?username=foo
  • A cookie, which is a bit of information sent by the server that the browser will then send on every request
  • A value in the Drupal/PHP Session (which itself is usually based on a cookie)

Either way, you will need to consider caching, so that Drupal knows about where you get the information from when caching the result.

You should also read up on services in Drupal and request/response handling (for example, Introducing the Symfony Request Object). (There are many other resources about request/response in Drupal, in Symfony, and just in general.) Understanding these concepts will help you understand what you need to use.

  • Oh I see, I didn't know that, thank you. Regarding the point about caching, does that mean I have to do something about caching even if I went the session route?
    – PHPDev
    Apr 9, 2018 at 21:01
  • Yes, because you need to tell drupal that your information depends on the session, otherwise your block/page might be cached and return the same information for all visitors. That's pretty easy though, just add session to the cache contexts in your element as explain in the documentation
    – Berdir
    Apr 10, 2018 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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