> For the complete documentation index, see [llms.txt](https://azutopia.gitbook.io/azu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://azutopia.gitbook.io/azu/endpoints/handle-parameters.md).

# Handle Parameters

This guide shows you how to extract and work with request parameters.

## Route Parameters

Define route parameters with a colon prefix:

```crystal
get "/users/:id"

def call
  id = params["id"]  # String
  id.to_i64          # Convert to Int64
end
```

### Multiple Route Parameters

```crystal
get "/users/:user_id/posts/:post_id"

def call
  user_id = params["user_id"].to_i64
  post_id = params["post_id"].to_i64
end
```

## Query Parameters

Access query string parameters:

```crystal
# URL: /search?q=crystal&page=2

get "/search"

def call
  query = params["q"]?            # "crystal" or nil
  page = params["page"]? || "1"   # "2" or default "1"
end
```

## Request Body (JSON)

Use request contracts to parse JSON bodies:

```crystal
struct CreateUserRequest
  include Azu::Request

  getter name : String
  getter email : String
  getter age : Int32?

  def initialize(@name = "", @email = "", @age = nil)
  end
end

struct CreateUserEndpoint
  include Azu::Endpoint(CreateUserRequest, UserResponse)

  post "/users"

  def call
    name = create_user_request.name
    email = create_user_request.email
    age = create_user_request.age
  end
end
```

## Form Data

Handle form submissions:

```crystal
struct FormEndpoint
  include Azu::Endpoint(FormRequest, FormResponse)

  post "/submit"

  def call
    # Access form fields from request contract
    name = form_request.name
    email = form_request.email
  end
end
```

## File Uploads

Handle multipart file uploads:

```crystal
struct UploadRequest
  include Azu::Request

  getter file : HTTP::FormData::File
  getter description : String?

  def initialize(@file, @description = nil)
  end
end

struct UploadEndpoint
  include Azu::Endpoint(UploadRequest, UploadResponse)

  post "/upload"

  def call
    file = upload_request.file
    filename = file.filename
    content = file.content
    content_type = file.content_type
  end
end
```

## Headers

Access request headers:

```crystal
def call
  auth = headers["Authorization"]?
  user_agent = headers["User-Agent"]?
  accept = headers["Accept"]?
end
```

## Type Conversion

Convert string parameters to types:

```crystal
def call
  # String to integer
  id = params["id"].to_i64

  # String to boolean
  active = params["active"]? == "true"

  # String to enum
  status = Status.parse(params["status"]? || "pending")
end
```

## Default Values

Provide defaults for optional parameters:

```crystal
def call
  page = (params["page"]? || "1").to_i
  per_page = (params["per_page"]? || "20").to_i
  sort = params["sort"]? || "created_at"
  order = params["order"]? || "desc"
end
```

## See Also

* [Validate Requests](/azu/validation/validate-requests.md)
* [Handle File Uploads](/azu/file-handling/handle-file-uploads.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://azutopia.gitbook.io/azu/endpoints/handle-parameters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
