Skip to content

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.

vb
Public Sub Text(Data As String)

Parameters:

  • Data - Response text content

Example:

vb
ctx.Response.Text "Hello, World!"

Html

Send HTML response.

vb
Public Sub Html(Data As String)

Parameters:

  • Data - HTML content

Example:

vb
ctx.Response.Html "<h1>Hello</h1><p>Welcome to my site</p>"

Json

Send JSON response.

vb
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 text
  • Count - Total data count (for pagination)
  • Whitespace - JSON formatting option

Example:

vb
' 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, , , , True

File

Send file response.

vb
Public Sub File(Path As String)

Parameters:

  • Path - File path (relative to WebRoot)

Example:

vb
' Request /image/logo.png
ctx.Response.File "/image/logo.png"

Status Code Methods

State

Send custom status code response.

vb
Public Sub State(Code As Long, Text As String, Optional Say As String = "Unknown status")

State400

400 Bad Request.

vb
Public Sub State400(Optional Say As String)

Example:

vb
If Not IsValidParams Then
    ctx.Response.State400 "Invalid parameters"
End If

State401

401 Unauthorized.

vb
Public Sub State401(Optional Say As String)

Example:

vb
If Not IsAuthenticated Then
    ctx.Response.State401 "Please login first"
End If

State403

403 Forbidden.

vb
Public Sub State403(Optional Say As String)

Example:

vb
If Not HasPermission Then
    ctx.Response.State403 "No permission"
End If

State404

404 Not Found.

vb
Public Sub State404(Optional Say As String)

Example:

vb
If Not UserExists(userId) Then
    ctx.Response.State404 "User not found"
End If

State500

500 Internal Server Error.

vb
Public Sub State500(Optional Say As String)

Example:

vb
On Error GoTo ErrorHandler
' ... Business logic
Exit Sub

ErrorHandler:
    ctx.Response.State500 Err.Description

State502

502 Gateway Error.

vb
Public Sub State502(Optional Say As String)

Configuration Properties

Custom response header dictionary.

vb
Public Header As New Scripting.Dictionary

Example:

vb
ctx.Response.Header("X-Custom-Header") = "CustomValue"
ctx.Response.Header("Cache-Control") = "no-cache"

CharSet

Character encoding (default utf-8).

vb
Public CharSet As String

Example:

vb
ctx.Response.CharSet = "gb2312"

ContentType

Content type.

vb
Public ContentType As String

Example:

vb
ctx.Response.ContentType = "application/xml"

Status

HTTP status string.

vb
Public Status As String

JsonPackFieldNameCode

JSON wrapper field name - status code.

vb
Public JsonPackFieldNameCode As String

Default: "code"


JsonPackFieldNameMsg

JSON wrapper field name - message.

vb
Public JsonPackFieldNameMsg As String

Default: "msg"


JsonPackFieldNameData

JSON wrapper field name - data.

vb
Public JsonPackFieldNameData As String

Default: "data"


JsonPackFieldNameCount

JSON wrapper field name - total count.

vb
Public JsonPackFieldNameCount As String

Default: "count"

Example:

vb
' 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", 10

ASP-compatible Properties

PropertyDescription
BufferOutput buffering
CacheControlCache control
CodePageCode page
ExpiresExpiration time
ExpiresAbsoluteAbsolute expiration time
LCIDLocale identifier

Last Updated: 2026-05-17

VB6 and LOGO copyright of Microsoft Corporation