These are the beta docs for the upcoming v0.10.0 release.

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>')
%>

Example Usage

Success Response

<%
const data = {
    user: {
        name: 'John Doe',
        email: 'john@example.com'
    }
}

response.json(200, data)
%>

Error Handling

<%
if (!params.id) {
    response.json(400, {
        error: 'Missing ID parameter'
    })
    return
}

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

File Downloads

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

Redirects

<%
if (!user.isAuthenticated) {
    response.redirect('/login')
    return
}

// Permanent redirect
response.redirect('/new-page', 301)
%>