HttpWrite

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.

Syntax

bool httpWrite ( string data [, int length] )

Shortcut syntax

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

[html]
<* = text *>

which is equivalent to:

[html]
<* 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

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

Example

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.

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

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())
	end
	fileClose(file)                           
	httpSetResponseHeader ( "content-type", "image/png")
else
	*>
	Could not read file
	<*
end
*>

See Also

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