response - HTTP Response Object

  • Type: PagesResponse
  • Description: The response object provides methods to control the HTTP response in a framework-agnostic way. This abstraction ensures your templates remain compatible across PocketBase versions.

Methods

file(path: string)

Serves a file from the filesystem.

<%
// Serve a PDF file
response.file('/path/to/document.pdf')
%>

write(s: string)

Writes a string directly to the response body.

<%
response.write('<h1>Hello World</h1>')
%>

redirect(path: string, status?: number)

Redirects to another URL, optionally specifying a status code.

<%
// Simple redirect
response.redirect('/dashboard')

// Redirect with status code
response.redirect('/login', 302)
%>

json(status: number, data: any)

Sends a JSON response with the specified status code.

<%
response.json(200, {
    success: true,
    data: { message: 'Hello World' }
})
%>

html(status: number, data: string)

Sends an HTML response with the specified status code.

<%
response.html(200, '<h1>Hello World</h1>')
%>

header(name: string, value?: string)

Gets or sets a response header. If value is provided, sets the header. If value is omitted, returns the current header value.

<%
// Set a header
response.header('Content-Type', 'application/json')
response.header('X-Custom-Header', 'custom-value')

// Get a header value
const contentType = response.header('Content-Type')
%>

cookie(name: string, value: string, options?: any)

Sets a cookie in the response. Returns the serialized cookie string.

<%
// Set a simple cookie
response.cookie('user_id', '123')

// Set a cookie with options
response.cookie('session', 'abc123', {
    httpOnly: true,
    secure: true,
    maxAge: 3600,
    path: '/'
})
%>

Example Usage

Complete Response with Headers

<%
// Set headers
response.header('Cache-Control', 'no-cache')
response.header('X-Custom-Header', 'custom-value')

// Set a cookie
response.cookie('session', 'xyz789', {
    httpOnly: true,
    secure: true
})

// Send JSON response
response.json(200, {
    success: true,
    data: { message: 'Operation completed' }
})
%>

Error Handling

<%
if (!params.id) {
    response.header('X-Error-Type', 'validation')
    response.json(400, {
        error: 'Missing ID parameter'
    })
    return
}

if (!record) {
    response.html(404, '<h1>Record Not Found</h1>')
    return
}
%>

File Download with Headers

<%
response.header('Content-Disposition', 'attachment; filename="document.pdf"')
response.file('/path/to/document.pdf')
%>