From Multi Theft Auto: Wiki
Jump to: navigation, search

This function adds text to the output of the current HTTP file of the HTTP interface. The function can only be used on parsed (i.e not marked as raw) HTTP pages. httpWrite can support outputing binary data, if you specify the length of the data you are outtputing. If you do this, you should ensure you set an accurate content-type using httpSetResponseHeader otherwise it may be displayed inconsistently by browsers.


bool httpWrite ( string data [, int length] )

Shortcut syntax

The following shortcut syntax is also supported (the space between the <* and the = is optional):

<* = text *>

which is equivalent to:

<* httpWrite(text) *>

Required Arguments

  • data: the data to be added to the page's output.

Optional Arguments

  • length: The length of the data being written. Generally only should be required for writing binary data.


Returns true if the text was added to the output buffer successfully, false otherwise.


Example 1: This sample resource page will output a random quote from a player using a function previously exported to http from a Lua script in the resource.

      <* = call ( getResourceFromName("ajax"), "start", getResourceName(getThisResource()) ) *>
      <b>Random quote:</b> <* httpWrite( call ( getThisResource(), "getRandomQuote" ) ) *>

The first httpWrite call (in shortcut syntax) adds the links to scripts generated by the ajax resource, enabling use of the getRandomQuote function.

The second httpWrite call writes the quote generated by the function to the HTML output.

Example 2: Using httpWrite to return a file read in with fileRead. This is useful as the internal server doesn't not support subdirectories reliably so you can create a page to return files to you.

local file = fileOpen ( "icons/icon.png" )
if file then
	while not fileIsEOF(file) do            
		buffer = fileRead(file, 500)         
		httpWrite(buffer, buffer:len())
	httpSetResponseHeader ( "content-type", "image/png")
	Could not read file

See Also

These functions can only be used from within lua blocks in HTML pages hosted by the server