HTTPRequestHeader
Returns the value of a specified HTTP request header from the inbound request. Notes that custom headers are returned even though the docs restrict it to standard headers.
Syntax
HTTPRequestHeader(headerToRetrieve) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
headerToRetrieve |
string | Yes | The name of the request header to read |
Example
%%[ VAR @ua SET @ua = HTTPRequestHeader("User-Agent") ]%%
%%=v(@ua)=%%
Renders the value of the caller’s User-Agent header, e.g. AmpProbe/1.0 for a request that set it.
Read a header and fall back when it is absent:
%%[
VAR @ref
SET @ref = HTTPRequestHeader("Referer")
IF Empty(@ref) THEN
SET @ref = "(direct visit)"
ENDIF
]%%
Referrer: %%=v(@ref)=%%
Return value
string — the value of the named request header, or the empty string when the request carried no such header. The value is arbitrary text, so there is no closed set of values to test for.
Behaviour
A present header returns its value; an absent header returns empty. A request whose User-Agent was set returned that exact string, and Host returned the CloudPage publish host. A Referer header that was not sent returned the empty string and Empty() reported true.
Custom headers are returned too, despite the documented restriction. The official reference says only the standard RFC 7231 headers can be retrieved, but a request sent with a custom X-Amp-Probe header returned that value verbatim — the restriction is not enforced at read time. See Differs from official docs.
Email/send context: rejected in sendable content
This function reads an inbound HTTP request, which only exists on a CloudPage / landing page. In a sendable email, an isolated %%=HTTPRequestHeader("User-Agent")=%% is rejected with HTTP 400, errorcode 10004: “HTTPRequestHeader Function is not valid in content. This function is only allowed in content with an HTTP context.” So it cannot be used inside a sendable email — request headers are a request-time value that does not exist at send time. See Differs from official docs.
Show test script
%%[
VAR @b, @v, @m1, @m2, @m3
SET @b = RequestParameter("b")
OutputLine(Concat("CTRL=[", Uppercase("ok"), "]"))
/* present standard header - send User-Agent, fetch ?b=ua */
IF @b == "ua" THEN
SET @v = HTTPRequestHeader("User-Agent")
OutputLine(Concat("ua=[", @v, "] empty=[", Empty(@v), "]"))
ENDIF
/* Host is always present on a CloudPage request. fetch ?b=host */
IF @b == "host" THEN
SET @v = HTTPRequestHeader("Host")
OutputLine(Concat("host=[", @v, "]"))
ENDIF
/* custom non-RFC7231 header - send X-Amp-Probe: probe-value-42, fetch ?b=custom */
IF @b == "custom" THEN
SET @v = HTTPRequestHeader("X-Amp-Probe")
OutputLine(Concat("custom=[", @v, "]"))
ENDIF
/* absent header - send no Referer, fetch ?b=absent, renders empty */
IF @b == "absent" THEN
SET @v = HTTPRequestHeader("Referer")
OutputLine(Concat("absent=[", @v, "] empty=[", Empty(@v), "]"))
ENDIF
/* several reads in one block all succeed. fetch ?b=multi */
IF @b == "multi" THEN
SET @m1 = HTTPRequestHeader("User-Agent")
SET @m2 = HTTPRequestHeader("Host")
SET @m3 = HTTPRequestHeader("Referer")
OutputLine(Concat("m1=[", @m1, "] m2=[", @m2, "] m3=[", @m3, "]"))
ENDIF
]%%
Availability
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | No |
See also
HTTPGet— other HTTP functions- Differs from official docs — custom headers are readable
- Official reference
- ampscript.guide