Skip to content

Multipart Data Utilities

Dickson Law edited this page Apr 9, 2023 · 1 revision

Multipart Data Utilities

MultipartDataBuilder(data)

This sets up a utility factory for generating the buffer body for a multipart/form-data request and its associated request headers. The data can be a struct or a JsonStruct. Example of usage:

var mpdb = new MultipartDataBuilder({
	title: "Twerking Turtle",
	description: "You know what to do with that big fat butt?",
	image: new FilePart(working_directory + "twerking_turtle.gif")
});
var bufferBody = mpdb.getBuffer();
var headers = mpdb.getHeaderMap();

The example above contains 2 keys containing plain strings and 1 key containing a file attachment part.

Fields

  • boundary: The boundary fragment used to separate parts. Is automatically generated, but can be manually set before calling getBuffer().
  • data: The struct being encoded into multipart/form-data.
  • size: The length of the resulting request buffer. Only available after getBuffer() is called.

Methods

  • getBuffer(): Encode the struct data into multipart/form-data and return the resulting buffer.
  • getHeaderMap(): Return a map containing the prerequisite headers for the encoded struct request body. Use only after getBuffer() is called!
  • writeHeaderMap(map): Write the prerequisite headers for the encoded struct request body to an existing map. Use only after getBuffer() is called!

File Part Classes

BufferFilePart(filename, buffer)

This represents a binary file attachment emulated by a buffer. Example:

new BufferFilePart("upload.bin", buffer)

Fields

  • buffer: The buffer representing the emulated file's body.
  • bufferStart: The position in the buffer where the emulated file's body starts. Default: 0
  • bufferSize: The length of the buffer to use as the emulated file's body. Default: buffer_tell(buffer)
  • filename: The name of the emulated file.
  • mimeType: The MIME type of the emulated file. Is automatically computed using the extension on filename, but can be manually specified if desired.

Methods

  • writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use by MultipartDataBuilder.getBuffer().

BufferPart(buffer)

This represents a normal part containing data from a buffer. Example:

new BufferPart(binData)

Fields

  • buffer: The buffer representing the part body.
  • bufferStart: The position in the buffer where the part's body starts. Default: 0
  • bufferSize: The length of the buffer to use as the part's body. Default: buffer_tell(buffer)

Methods

  • writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use by MultipartDataBuilder.getBuffer().

FilePart(filepath)

This represents a binary file attachment. Example:

new FilePart(working_directory + "twerking_turtle.gif")

On VM and YYC exports, this type can be used for both binary and text files. On HTML5, this type can only be used for emulated files saved using buffer-saving functions (which include files downloaded using xhr_download with textMode off).

Fields

  • filepath: The given path to the attached file.
  • mimeType: The MIME type of the file. Is automatically computed based on the extension on filepath, but can be manually specified if desired.

Methods

  • writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use by MultipartDataBuilder.getBuffer().

StringFilePart(filename, body)

This represents a text file attachment emulated by a string. Example:

new StringFilePart("example.txt", "Hello World!\nGoodbye World!")

Fields

  • body: The body of the emulated file.
  • filename: The name of the emulated file.
  • mimeType: The MIME type of the emulated file. Is automatically computed using the extension on filename, but can be manually specified if desired.

Methods

  • writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use by MultipartDataBuilder.getBuffer().

TextFilePart(filepath, <opts>)

This represents a text file attachment from an actual file. Example:

new TextFilePart(working_directory + "readme.md")

Fields

  • filepath: The given path to the attached file.
  • mimeType: The MIME type of the file. Is automatically computed based on the extension on filepath, but can be manually specified if desired.
  • newline: The newline used in the file. Default: "\r\n"
  • trailingNewline: Whether to end the file with a newline. Default: false

newline and trailingNewline can be optionally be set in the constructor using a struct passed as a parameter:

new TextFilePart(working_directory + "level1.data", { newline: "\n", trailingNewline: true })

Methods

  • writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use by MultipartDataBuilder.getBuffer().

Clone this wiki locally