<p>By checking the code, I could find a method named <code>getHeaders()</code> that will return all the headers of the current request.</p>

<p>So if you want to retrieve the content of the <code>foo</code> header, just use:</p>

<pre><code>{% set headers = craft.app.request.getHeaders() %}
{% set fooHeader = headers['foo'] %}
</code></pre>

CC BY-SA 4.0
2018-10-10T13:48:16.143Z
Worth noting that attempting to access the `headers` variable as an array will result in a Twig Runtime Error, specifically `Key "foo" in object with ArrayAccess of class "yii\web\HeaderCollection" does not exist.`. Using the `get` method for the `HeaderCollection` is recommend, as follows, `{{ headers.get('foo') }}`
zesda
CC BY-SA 4.0
2018-07-12T12:36:59.953Z
2018-07-12T17:46:01.490Z
2018-07-12T17:46:01.490Z
kant312
<p><a href="https://docs.craftcms.com/api/v3/craft-web-request.html#public-methods" rel="nofollow noreferrer"><code>craft.app.request.getHeaders()</code></a> returns an instance of <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-headercollection#methods" rel="nofollow noreferrer"><code>yii\web\HeaderCollection</code></a>, which has a number of public methods you can use from a Twig template.</p>
<p>For example, to see if an <code>isAjax</code> header is present:</p>
<pre class="lang-php-template prettyprint-override"><code>{# Get the header collection #}
{% set headers = craft.app.request.getHeaders() %}

{# Test for specific header #}
{% set isAjaxRequest = headers.has('isAjax') %}

{% if isAjaxRequest %}
  {# Do something... #}
{% endif %}
</code></pre>
<p>If you need to read the value of a header, use the <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-headercollection#get()-detail" rel="nofollow noreferrer"><code>get()</code></a> method.</p>

CC BY-SA 4.0
2021-12-06T17:17:12.500Z
2021-12-06T17:17:12.500Z
Josh Harrison
<p>I would like to detect if a header in the current request is present and obtain its value, directly within a Twig template. 
I know that <code>craft.app.request</code> gives access to a few variables like <code>isAjax</code> that directly check the headers content, but I haven't found a method to directly retrieve a specific header.</p>

<p>Can this be achieved in some way? (preferably not by writing a plugin)</p>

<craft3><request><https><headers>
From a Twig template, how to retrieve the content of a HTTP header in the current request?
# From a Twig template, how to retrieve the content of a HTTP header in the current request?

(Asked by: username_0 on 2018-07-12)

I would like to detect if a header in the current request is present and obtain its value, directly within a Twig template.
I know that `craft.app.request` gives access to a few variables like `isAjax` that directly check the headers content, but I haven't found a method to directly retrieve a specific header.

Can this be achieved in some way? (preferably not by writing a plugin)

**Tags:** craft3, request, https, headers

---

## 2 answers

### Answer by username_0 on 2018-07-12. Score: 4

By checking the code, I could find a method named getHeaders() that will return all the headers of the current request.
So if you want to retrieve the content of the foo header, just use:
{% set headers = craft.app.request.getHeaders() %}
{% set fooHeader = headers['foo'] %}

> **Comments:**
> **username_1**: Worth noting that attempting to access the `headers` variable as an array will result in a Twig Runtime Error, specifically `Key "foo" in object with ArrayAccess of class "yii\web\HeaderCollection" does not exist.`. Using the `get` method for the `HeaderCollection` is recommend, as follows, `{{ headers.get('foo') }}`  

---

### Answer by username_2 on 2021-12-06. Score: 2

craft.app.request.getHeaders() returns an instance of yii\web\HeaderCollection, which has a number of public methods you can use from a Twig template.
For example, to see if an isAjax header is present:
{# Get the header collection #}
{% set headers = craft.app.request.getHeaders() %}

{# Test for specific header #}
{% set isAjaxRequest = headers.has('isAjax') %}

{% if isAjaxRequest %}
  {# Do something... #}
{% endif %}

If you need to read the value of a header, use the get() method.


---

