NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_write(n) 5.0.0a naviserver "NaviServer Built-in Commands"

Name

ns_write - Return data to client

Table Of Contents

Synopsis

Description

These commands are used to manually construct and send an HTTP response piecewise (in a streaming manner), to the client.

COMMANDS

ns_headers ?-binary? ?--? status ?mimetype? ?length?

Sets the HTTP response status code and mimetype header for the data which is to be sent. The headers, including any extra headers set via the ns_conn outputheaders command, may not be flushed to the client until the first body data is written.

The -binary switch indicates that binary data is to be written, in which case the mimetype will be sent as given. Without this switch the mimetype will have the charset for the current connection's encoding appended. When length is specified, it will be set as content-length.

Returns 0.

ns_write data ...

Writes all data directly to the client. No HTTP headers are sent unless ns_headers has been called.

If data is a Tcl byte-array object or the -binary option was given to ns_headers then no transcoding will take place. Otherwise, the encoding in effect for the current connection will be used to encode data.

If an HTTP response is being constructed (ns_headers has been called) and the client supports it, HTTP chunking will be used to stream data on each call to ns_write. If the client does not support HTTP chunking, connection keep-alive is disabled. Prefer to send a complete response using e.g. ns_return if possible.

ns_write returns true if all the data was written successfully and false otherwise. On failure, no more writes should be attempted.

After the command completes the connection remains open and available in the calling connection thread.

The ns_adp_puts command is similar to ns_write, but there are important differences. While ns_write writes directly to the client's connection and allows you to send header fields and control the output stream at a low-level, ns_adp_puts operates at a higher level by writing to the ADP output buffer and automatically appending newlines by default. Therefore, ns_adp_puts should be used for output within ADP pages, as it integrates seamlessly with the ADP processing model and ensures proper output buffering.

ns_writefp channelId ?nbytes?

Writes the contents of the specified file to the connection. One can specify the number of bytes to be copied in the nbytes argument. By default, the entire file is transmitted.

EXAMPLES

Report results progressively:

 ns_register_proc GET /long-running-process {
   ns_headers 200 text/plain
   ns_write "Results: \n"
 
   while {[do_process result]} {
     ns_write "$result\n"
   }
 
   ns_write "Done."
 }

Sending binary data:

 ns_register_proc GET /blobs {
   ...
   ns_headers -binary -- 200 application/x-whatever
   ns_write $blob1 $blob2
   ...
   ns_write $blob3
 }

Sending data to the connection from a file:

 ns_register_proc GET /sampleWriteFp {
   ns_headers -- 200 text/plain
   set file [open "/usr/local/ns/copyright.txt" r]
   ns_writefp $file
   close $file
 }

See Also

ns_adp_puts, ns_conn, ns_register, ns_return

Keywords

charset, encoding, global built-in, response, return, server built-in, status, writer