Request

The HTTP request is available via c.Request — a standard *http.Request. Nimbus does not wrap it with a custom type, so you have full access to every method and field the Go standard library provides.

Request Headers

Read any HTTP header using c.Request.Header.Get():

contentType := c.Request.Header.Get("Content-Type")
auth := c.Request.Header.Get("Authorization")
accept := c.Request.Header.Get("Accept")
custom := c.Request.Header.Get("X-Custom-Header")

Query Parameters

Access URL query string values with c.Request.URL.Query():

// URL: /search?q=nimbus&page=2
query := c.Request.URL.Query()
q := query.Get("q")       // "nimbus"
page := query.Get("page") // "2"

// Check if a param exists
if query.Has("sort") {
    sort := query.Get("sort")
    // use sort
}

Route Parameters

Named path segments are extracted by the router and available via c.Param():

// Route: /users/:id/posts/:postId
id := c.Param("id")
postId := c.Param("postId")

Form Data

For application/x-www-form-urlencoded or multipart/form-data bodies, use ParseForm() or ParseMultipartForm():

c.Request.ParseForm()
name := c.Request.FormValue("name")
email := c.Request.FormValue("email")

// Multipart form (file uploads)
c.Request.ParseMultipartForm(10 << 20) // 10 MB max
file, header, err := c.Request.FormFile("avatar")

JSON Body

Decode a JSON request body into a Go struct using json.NewDecoder:

type CreatePostInput struct {
    Title string
    Body  string
}

func createPost(c *http.Context) error {
    var input CreatePostInput
    err := json.NewDecoder(c.Request.Body).Decode(&input)
    if err != nil {
        return c.JSON(400, map[string]string{"error": "invalid JSON"})
    }
    defer c.Request.Body.Close()
    return c.JSON(201, input)
}

URL Path and Method

Get the request path and HTTP method:

path := c.Request.URL.Path     // "/users/42"
method := c.Request.Method      // "GET", "POST", etc.
fullURL := c.Request.URL.String() // "/users/42?tab=profile"

Cookies

Read cookies from the request:

cookie, err := c.Request.Cookie("session_id")
if err == nil {
    sessionID := cookie.Value
    // use sessionID
}

// Set a cookie on the response
http.SetCookie(c.Response, &http.Cookie{
    Name:     "session_id",
    Value:    newSessionID,
    Path:     "/",
    HttpOnly: true,
    MaxAge:   86400,
})

Client IP Address

Get the client IP from RemoteAddr, or check forwarded headers when behind a proxy:

ip := c.Request.RemoteAddr

// Behind a reverse proxy, prefer X-Forwarded-For or X-Real-IP
if forwarded := c.Request.Header.Get("X-Forwarded-For"); forwarded != "" {
    ip = forwarded
}
if realIP := c.Request.Header.Get("X-Real-IP"); realIP != "" {
    ip = realIP
}

Content Type

Check the content type of the incoming request to handle different body formats:

ct := c.Request.Header.Get("Content-Type")
// "application/json", "application/x-www-form-urlencoded", "multipart/form-data", etc.

Full Example

A handler that reads multiple request properties:

func debugHandler(c *http.Context) error {
    info := map[string]string{
        "method":      c.Request.Method,
        "path":        c.Request.URL.Path,
        "query":       c.Request.URL.RawQuery,
        "ip":          c.Request.RemoteAddr,
        "user_agent":  c.Request.Header.Get("User-Agent"),
        "content_type": c.Request.Header.Get("Content-Type"),
    }
    return c.JSON(200, info)
}