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 Scripting.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.
Public Sub File(Path As String)Parameters:
Path- File path (relative to WebRoot)
Example:
' Request /image/logo.png
ctx.Response.File "/image/logo.png"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 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 Scripting.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-05-17