# Request

Every route handler in Slush receives a `request` object as an argument.

## Basic Usage

```python
@app.route("/hello", methods=["GET"])
def hello(request):
    return {"message": "Hello from Slush"}
```

The `request` object gives you access to everything sent by the client.

## Accessing Query Parameters

Query parameters are available via `request.query_params`.

```python
@app.route("/search", methods=["GET"])
def search(request):
    query = request.query_params.get("q", "")
    limit = int(request.query_params.get("limit", 10))
    return {"query": query, "limit": limit}
```

Example request:

```
/search?q=python&limit=5
```

## Accessing JSON Body

For JSON requests, use `request.json`.

```python
@app.route("/user", methods=["POST"])
def create_user(request):
    data = request.json
    return {"name": data["name"]}
```

If the request body is invalid JSON, `request.json` will be `None`.

## Accessing Raw Body

You can access the raw request body using:

```python
request.body
```

This returns the body as a string.

## Handling File Uploads

Uploaded files can be accessed using `request.files()`:

```python
@app.route("/upload", methods=["POST"])
def upload(request):
    files = request.files()

    if "file" not in files:
        return {"error": "No file uploaded"}

    uploaded = files["file"]
    uploaded.save()

    return {
        "filename": uploaded.filename,
        "size": uploaded.size,
        "type": uploaded.content_type,
    }
```

Each file is returned as an `UploadedFile` object.

## Accessing Form Data

For form submissions:

```python
form_data = request.form()
```

Example:

```python
@app.route("/submit", methods=["POST"])
def submit(request):
    data = request.form()
    return {"data": data}
```

## Accessing Headers

Request headers are available as a dictionary:

```python
headers = request.headers
```

Example:

```python
@app.route("/headers", methods=["GET"])
def headers(request):
    return {"user-agent": request.headers.get("User-Agent")}
```

## Accessing Cookies

Cookies can be accessed using:

```python
cookies = request.cookies
```

Example:

```python
@app.route("/profile", methods=["GET"])
def profile(request):
    session_id = request.cookies.get("session_id")
    return {"session_id": session_id}
```

## Request Metadata

You can also access basic request information:

```python
request.method   # GET, POST, etc.
request.path     # URL path
```

Example:

```python
@app.route("/info", methods=["GET"])
def info(request):
    return {
        "method": request.method,
        "path": request.path
    }
```

## Summary

The `request` object provides:

* Query parameters via `request.query_params`
* JSON body via `request.json`
* Raw body via `request.body`
* File uploads via `request.files()`
* Form data via `request.form()`
* Headers via `request.headers`
* Cookies via `request.cookies`
* Request metadata like `method` and `path`

## Next Steps

* Explore **Middleware** for request/response processing


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
