API: response
Source:
http/response.ts
Provides a static façade (Response class) for managing the HTTP response. This class wraps a native Java HTTP response object, offering methods for setting status codes, headers, cookies, and writing content (text, JSON, or binary).
Usage
import { response } from "sdk/http";
response.println("Hello World!");
response.flush();
response.close();Classes
Response
The static Response class providing standardized HTTP status codes and methods
for constructing the server's response.
Methods
isValid
isValid ():booleanChecks if the response façade is currently valid or connected to an active request context.
@returns True if valid, false otherwise.
json
json (obj:any):voidSerializes a JavaScript object to JSON, sets the
Content-Type: application/jsonheader,
and writes the JSON string to the response output stream.
@param obj The JavaScript object to be serialized and sent.
print
print (text:string):voidWrites a string of text to the response body using UTF-8 encoding.
Note: This method automatically handles flushing the output stream.
@param text The string content to write.
println
println (text:string):voidWrites a string of text followed by a newline character (
\n) to the response body
using UTF-8 encoding.
@param text The string content to write.
write
write (bytes:any[]):voidWrites an array of bytes directly to the response output stream, typically used for binary data.
@param bytes The array of bytes to write.
isCommitted
isCommitted ():booleanChecks if the response headers and status have already been sent to the client.
@returns True if the response is committed, false otherwise.
setContentType
setContentType (contentType:string):voidSets the value of the
Content-Typeheader.
@param contentType The MIME type string (e.g., 'text/html', 'application/pdf').
flush
flush ():voidForces any buffered output to be written to the client.
close
close ():voidCloses the response output stream.
addCookie
addCookie (cookie:Cookie):voidAdds a cookie to the response. The cookie object is serialized to JSON before being passed
to the underlying Java facade.
@param cookie The cookie definition object.
containsHeader
containsHeader (name:string):booleanChecks if a response header with the specified name has already been set.
@param name The name of the header.
@returns True if the header exists, false otherwise.
encodeURL
encodeURL (url:string):stringEncodes a URL for use in redirects or forms, including session information if necessary.
@param url The URL to encode.
@returns The encoded URL string.
getCharacterEncoding
getCharacterEncoding ():stringGets the character encoding used for the response body.
@returns The character encoding string.
encodeRedirectURL
encodeRedirectURL (url:string):stringEncodes a URL for use in the
Locationheader of a redirect response.
@param url The redirect URL to encode.
@returns The encoded redirect URL string.
getContentType
getContentType ():stringGets the current
Content-Typeheader value.
@returns The content type string.
sendError
sendError (status:number, message?:string):voidSends an HTTP error response to the client with the specified status code and optional message.
This bypasses the normal response body writing process.
@param status The HTTP status code (e.g., 404, 500).
@param message An optional message to include in the error response.
setCharacterEncoding
setCharacterEncoding (charset:string):voidSets the character encoding to be used for the response body (e.g., 'UTF-8').
@param charset The character set string.
sendRedirect
sendRedirect (location:string):voidSends a redirect response (status code 302 by default) to the client.
@param location The new URL to redirect the client to.
setContentLength
setContentLength (length:number):voidSets the
Content-Lengthheader for the response.
@param length The size of the response body in bytes.
setHeader
setHeader (name:string, value:string):voidSets a response header with the given name and value. If the header already exists, its value is overwritten.
@param name The name of the header.
@param value The value of the header.
addHeader
addHeader (name:string, value:string):voidAdds a response header with the given name and value. If the header already exists, a second header with the same name is added.
@param name The name of the header.
@param value The value of the header.
setStatus
setStatus (status:number):voidSets the HTTP status code for the response.
@param status The integer status code (e.g., 200, 404).
reset
reset ():voidClears all buffers, status code, and headers from the response, allowing a new response to be generated.
This is only possible if the response has not yet been committed.
getHeader
getHeader (name:string):stringGets the value of a specific header. If multiple headers with the same name exist, it returns the first one.
@param name The name of the header.
@returns The header value string.
setLocale
setLocale (language:string, country?:string, variant?:string):voidSets the locale for the response, which may affect language and date/time formatting.
@param language The language code (e.g., 'en', 'fr').
@param country The optional country code (e.g., 'US', 'GB').
@param variant The optional variant code.
getHeaders
getHeaders (name:string):string[]Gets all header values for a specific header name as an array of strings.
@param name The name of the header.
@returns An array of header values.
getHeaderNames
getHeaderNames ():string[]Gets the names of all headers that have been set on the response.
@returns An array of header names.
getLocale
getLocale ():stringGets the currently set locale string for the response.
@returns The locale string.
getOutputStream
getOutputStream ():OutputStreamGets the underlying output stream object, wrapped in the SDK's
OutputStreamclass.
This is useful for writing raw or large amounts of data.
@returns The output stream object.