cHttpServerResponse Response Object
Overview
cHttpServerResponse is used to build and send HTTP responses, supporting text, HTML, JSON, files, and other response types.
Response Methods
Text
Send plain text response.
Public Sub Text(Data As String)Parameters:
Data- Response text content
Example:
ctx.Response.Text "Hello, World!"Html
Send HTML response.
Public Sub Html(Data As String)Parameters:
Data- HTML content
Example:
ctx.Response.Html "<h1>Hello</h1><p>Welcome to my site</p>"Json
Send JSON response.
Public Sub Json(Data As Variant, Optional Code As Long = -1, Optional msg As String, Optional Count As Long, Optional Whitespace As Variant)Parameters:
Data- Data to serialize to JSON (Dictionary, Array, etc.)Code- Status code (default -1 means no wrapper)msg- Message textCount- Total data count (for pagination)Whitespace- JSON formatting option
Example:
' Simple JSON
Dim data As New Dictionary
data("name") = "John"
data("age") = 25
ctx.Response.Json data
' Wrapped format (API standard response)
' Output: {"code": 0, "msg": "Success", "data": {...}, "count": 1}
ctx.Response.Json data, 0, "Success", 1
' Formatted output
ctx.Response.Json data, , , , TrueFile
Send file response (with built-in ETag + 304 negotiated cache + lazy content cache).
Public Sub File(Path As String)Parameters:
Path- File path (relative to WebRoot)
Description:
- Auto-calculates ETag (based on file modification time + size) and sets
ETag,Last-Modifiedresponse headers - When browser sends
If-None-Matchthat matches, returns304 Not Modified(zero transfer) - When file size ≤
MaxCacheFileSize, auto-adds to memory cache (lazy cache), subsequent access outputs from memory skipping disk I/O - Files exceeding cache limit are still read from disk, but enjoy ETag/304 negotiated cache
- When file is updated on disk, ETag auto-changes and old cache auto-invalidates
- When path corresponds to a directory, auto-searches default documents (index.html etc); returns 403 if no default document found
Example:
' Send static file (auto with cache headers)
ctx.Response.File "/css/style.css"
' Access directory (auto-searches index.html)
ctx.Response.File "/"
' Large file (exceeds cache limit, always read from disk, but with ETag/304)
ctx.Response.File "/videos/demo.mp4"Cache Mechanism Details:
Request → Calculate ETag (FSO metadata, no file content read)
├─> If-None-Match matches → 304 (zero transfer)
├─> Cache hit + ETag unchanged → Memory output (skip disk)
├─> Cache hit + ETag changed → Re-read + update cache
└─> No cache → Disk read + lazy cache (only add if ≤ limit)See Static File Serving - Cache Control
Status Code Methods
State
Send custom status code response.
Public Sub State(Code As Long, Text As String, Optional Say As String = "Unknown status")State400
400 Bad Request.
Public Sub State400(Optional Say As String)Example:
If Not IsValidParams Then
ctx.Response.State400 "Invalid parameters"
End IfState401
401 Unauthorized.
Public Sub State401(Optional Say As String)Example:
If Not IsAuthenticated Then
ctx.Response.State401 "Please login first"
End IfState403
403 Forbidden.
Public Sub State403(Optional Say As String)Example:
If Not HasPermission Then
ctx.Response.State403 "No permission"
End IfState404
404 Not Found.
Public Sub State404(Optional Say As String)Example:
If Not UserExists(userId) Then
ctx.Response.State404 "User not found"
End IfState413
413 Payload Too Large.
Public Sub State413(Optional Say As String = "Request Entity Too Large")Description: This status code is automatically returned when the request body exceeds the Server.MaxRequestSize limit. Can also be called manually.
Example:
' Check upload file size
If FileSize > MaxAllowedSize Then
ctx.Response.State413 "File size exceeds limit"
End IfState500
500 Internal Server Error.
Public Sub State500(Optional Say As String)Example:
On Error GoTo ErrorHandler
' ... Business logic
Exit Sub
ErrorHandler:
ctx.Response.State500 Err.DescriptionState502
502 Gateway Error.
Public Sub State502(Optional Say As String)Configuration Properties
Header
Custom response header dictionary.
Public Header As New DictionaryExample:
ctx.Response.Header("X-Custom-Header") = "CustomValue"
ctx.Response.Header("Cache-Control") = "no-cache"CharSet
Character encoding (default utf-8).
Public CharSet As StringExample:
ctx.Response.CharSet = "gb2312"ContentType
Content type.
Public ContentType As StringExample:
ctx.Response.ContentType = "application/xml"Status
HTTP status string.
Public Status As StringJsonPackFieldNameCode
JSON wrapper field name - status code.
Public JsonPackFieldNameCode As StringDefault: "code"
JsonPackFieldNameMsg
JSON wrapper field name - message.
Public JsonPackFieldNameMsg As StringDefault: "msg"
JsonPackFieldNameData
JSON wrapper field name - data.
Public JsonPackFieldNameData As StringDefault: "data"
JsonPackFieldNameCount
JSON wrapper field name - total count.
Public JsonPackFieldNameCount As StringDefault: "count"
Example:
' Custom JSON response field names
ctx.Response.JsonPackFieldNameCode = "status"
ctx.Response.JsonPackFieldNameMsg = "message"
ctx.Response.JsonPackFieldNameData = "result"
ctx.Response.JsonPackFieldNameCount = "total"
' Output: {"status": 0, "message": "OK", "result": {...}, "total": 10}
ctx.Response.Json data, 0, "OK", 10ASP-compatible Properties
| Property | Description |
|---|---|
Buffer | Output buffering |
CacheControl | Cache control |
CodePage | Code page |
Expires | Expiration time |
ExpiresAbsolute | Absolute expiration time |
LCID | Locale identifier |
Last Updated: 2026-06-22