> ## Documentation Index
> Fetch the complete documentation index at: https://ngrok.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# HTTP Request Variables

> Reference documentation for HTTP request variables in Traffic Policy including headers, method, URL, body, and user agent information.

## Request Variables

The following variables are available under the `req` namespace:

| Name                                                        | Type                  | Description                                                                                                                                                                                     |
| ----------------------------------------------------------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`req.content_encoding`](#reqcontent-encoding)              | `list[string]`        | The encodings set in the `Content-Encoding` header for this request.                                                                                                                            |
| [`req.content_length`](#reqcontent-length)                  | `int64`               | The content length of the body in bytes. This may not be present if the request does not contain a body or if the client does not specify a content length because they are streaming the body. |
| [`req.content_type`](#reqcontent-type)                      | `string`              | The media type set in the `Content-Type` header for this request.                                                                                                                               |
| [`req.content_type.parameters`](#reqcontent-typeparameters) | `map[string]string`   | The parameters set in the `Content-Type` header for this request.                                                                                                                               |
| [`req.content_type.raw`](#reqcontent-typeraw)               | `string`              | The value of the `Content-Type` header for this request.                                                                                                                                        |
| [`req.cookies`](#reqcookies)                                | `map[string][]cookie` | The HTTP cookie objects included in this request. If there are multiple cookies with the same name, they will be ordered as specified in the `Cookie` header.                                   |
| [`req.cookies[k][i].name`](#reqcookieskiname)               | `string`              | The name of the cookie.                                                                                                                                                                         |
| [`req.cookies[k][i].value`](#reqcookieskivalue)             | `string`              | The value of the cookie.                                                                                                                                                                        |
| [`req.headers`](#reqheaders)                                | `map[string][]string` | The request headers parsed as a map of lower-case names to values.                                                                                                                              |
| [`req.host`](#reqhost)                                      | `string`              | The value of the host header field for this request.                                                                                                                                            |
| [`req.location`](#reqlocation)                              | `string`              | The value of the Location header for this request.                                                                                                                                              |
| [`req.method`](#reqmethod)                                  | `string`              | The method for this request.                                                                                                                                                                    |
| [`req.trailers`](#reqtrailers)                              | `map[string][]string` | The request trailers parsed as a map of lower-case names to values.                                                                                                                             |
| [`req.ts.body_received`](#reqtsbody-received)               | `timestamp`           | The timestamp when ngrok received the body of the request. This may not be present if the request does not contain a body.                                                                      |
| [`req.ts.header_received`](#reqtsheader-received)           | `timestamp`           | The timestamp when ngrok received the header of the request.                                                                                                                                    |
| [`req.url`](#requrl)                                        | `string`              | The normalized full URL for this request.                                                                                                                                                       |
| [`req.url.authority`](#requrlauthority)                     | `string`              | The authority portion of the URL.                                                                                                                                                               |
| [`req.url.host`](#requrlhost)                               | `string`              | The hostname portion of the host for this request.                                                                                                                                              |
| [`req.url.path`](#requrlpath)                               | `string`              | The path for this request including the leading forward slash.                                                                                                                                  |
| [`req.url.port`](#requrlport)                               | `int32`               | The port portion of the URL. This may not be present if the URL does not explicitly specify a port.                                                                                             |
| [`req.url.query`](#requrlquery)                             | `string`              | The full query string for this request excluding the leading question mark.                                                                                                                     |
| [`req.url.query_params`](#requrlquery-params)               | `map[string][]string` | The request query string parsed as a map of names to values.                                                                                                                                    |
| [`req.url.raw`](#requrlraw)                                 | `string`              | The un-normalized full URL for this request.                                                                                                                                                    |
| [`req.url.raw_path`](#requrlraw-path)                       | `string`              | The un-normalized path including the leading slash for this request.                                                                                                                            |
| [`req.url.scheme`](#requrlscheme)                           | `string`              | The scheme for this request.                                                                                                                                                                    |
| [`req.url.uri`](#requrluri)                                 | `string`              | The URI (path and query) portion of the URL.                                                                                                                                                    |
| [`req.url.user_password`](#requrluser-password)             | `string`              | The `user:password` portion of the URL.                                                                                                                                                         |
| [`req.user_agent`](#requser-agent)                          | `string`              | The value of the `User-Agent` header for this request.                                                                                                                                          |
| [`req.user_agent.name`](#requser-agentname)                 | `string`              | The name of the user agent in the `User-Agent` header for this request.                                                                                                                         |
| [`req.user_agent.version`](#requser-agentversion)           | `string`              | The version of the user agent in the `User-Agent` header for this request.                                                                                                                      |
| [`req.user_agent.os`](#requser-agentos)                     | `string`              | The operating system of the user agent in the `User-Agent` header for this request.                                                                                                             |
| [`req.user_agent.os_version`](#requser-agentos-version)     | `string`              | The operating system of the user agent in the `User-Agent` header for this request.                                                                                                             |
| [`req.user_agent.device`](#requser-agentdevice)             | `string`              | The device in the `User-Agent` header for this request.                                                                                                                                         |
| [`req.user_agent.is_mobile`](#requser-agentis-mobile)       | `bool`                | Whether the `User-Agent` header for this request is a mobile device.                                                                                                                            |
| [`req.user_agent.is_tablet`](#requser-agentis-tablet)       | `bool`                | Whether the `User-Agent` header for this request is a tablet device.                                                                                                                            |
| [`req.user_agent.is_desktop`](#requser-agentis-desktop)     | `bool`                | Whether the `User-Agent` header for this request is a desktop device.                                                                                                                           |
| [`req.user_agent.is_bot`](#requser-agentis-bot)             | `bool`                | Whether the `User-Agent` header for this request is a Bot User.                                                                                                                                 |
| [`req.user_agent.url`](#requser-agenturl)                   | `string`              | The URL of the user agent in the `User-Agent` header for this request.                                                                                                                          |
| [`req.version`](#reqversion)                                | `string`              | The HTTP version for this request.                                                                                                                                                              |

### `req.content_encoding`

The encoding set in the `Content-Encoding` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.content_encoding[0] == 'br'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.content_encoding[0] == 'br'"
    ]
  }
  ```
</CodeGroup>

### `req.content_length`

The content length of the body in bytes. This may not be present if the request does not contain a body or if the client does not specify a content length because they are streaming the body.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.content_length > 10000000"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.content_length > 10000000"
    ]
  }
  ```
</CodeGroup>

### `req.content_type`

The media type set in the `Content-Type` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.content_type == 'application/json'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.content_type == 'application/json'"
    ]
  }
  ```
</CodeGroup>

### `req.content_type.parameters`

The parameters set in the `Content-Type` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.content_type.parameters['charset'] == 'utf-8'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.content_type.parameters['charset'] == 'utf-8'"
    ]
  }
  ```
</CodeGroup>

### `req.content_type.raw`

The value of the `Content-Type` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.content_type.raw == 'application/json; charset=utf-8'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.content_type.raw == 'application/json; charset=utf-8'"
    ]
  }
  ```
</CodeGroup>

### `req.cookies`

The HTTP cookie objects included in this request. If there are multiple cookies with the same name, they will be ordered as specified in the `Cookie` header.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "size(req.cookies) > 0"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "size(req.cookies) > 0"
    ]
  }
  ```
</CodeGroup>

### `req.cookies[k][i].name`

The name of the cookie.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.cookies['sessionId'][0].name == 'sessionId'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.cookies['sessionId'][0].name == 'sessionId'"
    ]
  }
  ```
</CodeGroup>

### `req.cookies[k][i].value`

The value of the cookie.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.cookies['sessionId'][0].value > 'value'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.cookies['sessionId'][0].value > 'value'"
    ]
  }
  ```
</CodeGroup>

### `req.headers`

The request headers parsed as a map of lower-case names to values.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "'Fizz' in req.headers['baz']"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "'Fizz' in req.headers['baz']"
    ]
  }
  ```
</CodeGroup>

### `req.host`

The value of the host header field value for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.host == 'nba.com'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.host == 'nba.com'"
    ]
  }
  ```
</CodeGroup>

### `req.location`

The value of the Location header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.location == '/index.html'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.location == '/index.html'"
    ]
  }
  ```
</CodeGroup>

### `req.method`

The method for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.method == 'POST' || req.method == 'PUT'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.method == 'POST' || req.method == 'PUT'"
    ]
  }
  ```
</CodeGroup>

### `req.trailers`

The request trailers parsed as a map of lower-case names to values.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "'Fizz' in req.trailers['baz']"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "'Fizz' in req.trailers['baz']"
    ]
  }
  ```
</CodeGroup>

### `req.ts.body_received`

The timestamp when ngrok received the body of the request. This may not be present if the request does not contain a body.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.ts.body_received > timestamp('2023-12-31T00:00:00Z')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.ts.body_received > timestamp('2023-12-31T00:00:00Z')"
    ]
  }
  ```
</CodeGroup>

### `req.ts.header_received`

The timestamp when ngrok received the header of the request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.ts.header_received > timestamp('2023-12-31T00:00:00Z')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.ts.header_received > timestamp('2023-12-31T00:00:00Z')"
    ]
  }
  ```
</CodeGroup>

### `req.url`

The normalized full URL for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url == 'https://nba.com/')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url == 'https://nba.com/')"
    ]
  }
  ```
</CodeGroup>

### `req.url.authority`

The authority portion of the URL.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.authority == 'user:password@nba.com'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.authority == 'user:password@nba.com'"
    ]
  }
  ```
</CodeGroup>

### `req.url.host`

The hostname portion of the host for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.host == 'nba.com'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.host == 'nba.com'"
    ]
  }
  ```
</CodeGroup>

### `req.url.path`

The path for this request including the leading forward slash.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.path.startsWith('/foo')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.path.startsWith('/foo')"
    ]
  }
  ```
</CodeGroup>

### `req.url.port`

The port portion of the URL. This may not be present if the URL does not explicitly specify a port.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.port == 443"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.port == 443"
    ]
  }
  ```
</CodeGroup>

### `req.url.query`

The full query string for this request excluding the leading question mark.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.query == 'number=23&name=jordan'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.query == 'number=23&name=jordan'"
    ]
  }
  ```
</CodeGroup>

### `req.url.query_params`

The request query string parsed as a map of names to values.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "'bar' in req.url.query_params['foo']"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "'bar' in req.url.query_params['foo']"
    ]
  }
  ```
</CodeGroup>

### `req.url.raw`

The un-normalized full URL for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.raw.includes('/foo')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.raw.includes('/foo')"
    ]
  }
  ```
</CodeGroup>

### `req.url.raw_path`

The un-normalized path including the leading slash for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.raw_path.startsWith('/foo')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.raw_path.startsWith('/foo')"
    ]
  }
  ```
</CodeGroup>

### `req.url.scheme`

The scheme for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.scheme == 'https'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.scheme == 'https'"
    ]
  }
  ```
</CodeGroup>

### `req.url.uri`

The URI (path and query) portion of the URL.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.uri.contains('/api/players?number=23&name=jordan')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.uri.contains('/api/players?number=23&name=jordan')"
    ]
  }
  ```
</CodeGroup>

### `req.url.user_password`

The `user:password` portion of the URL.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.url.user_password == 'user:password'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.url.user_password == 'user:password'"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.contains('curl')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.contains('curl')"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.name`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.name == 'Safari'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.name == 'Safari'"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.version`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.version == '10.1.2'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.version == '10.1.2'"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.os`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.os == 'macOS'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.os == 'macOS'"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.os_version`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.os_version == '10.12.6'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.os_version == '10.12.6'"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.device`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.device.contains('Android')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.device.contains('Android')"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.is_mobile`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.is_mobile"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.is_mobile"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.is_tablet`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.is_tablet"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.is_tablet"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.is_desktop`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.is_desktop"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.is_desktop"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.is_bot`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.is_bot"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.is_bot"
    ]
  }
  ```
</CodeGroup>

### `req.user_agent.url`

The value of the `User-Agent` header for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.user_agent.url.contains('https')"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.user_agent.url.contains('https')"
    ]
  }
  ```
</CodeGroup>

### `req.version`

The HTTP version for this request.

<CodeGroup>
  ```yaml policy.yml theme={null}
  expressions:
    - "req.version == 'HTTP/2.0'"
  ```

  ```json policy.json theme={null}
  {
    "expressions": [
      "req.version == 'HTTP/2.0'"
    ]
  }
  ```
</CodeGroup>
