# Request

```python
gradio.Request(···)
```

### Description

A Gradio request object that can be used to access the request headers, cookies, query parameters and other information about the request from within the prediction function. The class is a thin wrapper around the fastapi.Request class. Attributes of this class include: `headers`, `client`, `query_params`, `session_hash`, and `path_params`. If auth is enabled, the `username` attribute can be used to get the logged in user. In some environments, the dict-like attributes (e.g. `requests.headers`, `requests.query_params`) of this class are automatically converted to dictionaries, so we recommend converting them to dictionaries before accessing attributes for consistent behavior in different environments.

### Example Usage

```python
import gradio as gr
def echo(text, request: gr.Request):
    if request:
        print("Request headers dictionary:", request.headers)
        print("IP address:", request.client.host)
        print("Query parameters:", dict(request.query_params))
        print("Session hash:", request.session_hash)
    return text
io = gr.Interface(echo, "textbox", "textbox").launch()
```

### Initialization

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `request` | `fastapi.Request \| None` | `None` | A fastapi.Request |
| `username` | `str \| None` | `None` | The username of the logged in user (if auth is enabled) |
| `session_hash` | `str \| None` | `None` | The session hash of the current session. It is unique for each page load. |
### Demos

**request_ip_headers**

[See demo on Hugging Face Spaces](https://huggingface.co/spaces/gradio/request_ip_headers)

```python
import gradio as gr

def predict(text, request: gr.Request):
    headers = request.headers
    host = request.client.host
    user_agent = request.headers["user-agent"]
    return {
        "ip": host,
        "user_agent": user_agent,
        "headers": headers,
    }

gr.Interface(predict, "text", "json", api_name="predict").launch()
```
