node-libcurl

    Class CurlMimePart

    CurlMimePart class that represents a single part in a MIME structure.

    C++ source code

    This class is used to configure individual parts of a multipart form data upload. Instances are created by calling CurlMime.addPart.

    Each part can have:

    All setter methods return this for method chaining and throw CurlEasyError on error.

    Basic text field with method chaining:

    mime.addPart()
    .setName('username')
    .setData('john_doe')

    File upload with method chaining:

    mime.addPart()
    .setName('avatar')
    .setFileData('/path/to/image.png')
    .setType('image/png')
    .setFileName('avatar.png')
    Index

    Constructors

    Methods

    • Sets the data content for this MIME part from a string or Buffer.

      Parameters

      • data: null | string | Buffer<ArrayBufferLike>

        The data as a string or Buffer, or null to reset

      Returns this

      this for method chaining

      Throws on error

      The data is copied internally, so it's safe to reuse or modify the buffer after calling this method.

      part
      .setName('file')
      .setData('Hello World')
      .setType('text/plain')
      // or
      part
      .setName('file')
      .setData(Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]))
      .setType('text/plain')
    • Sets a callback-based mechanism for supplying part content dynamically.

      Parameters

      • size: number

        Expected total size in bytes, -1 works for unknown sizes

      • callbacks: MimeDataCallbacks

        Object containing read, seek (optional), and free (optional) callbacks

      Returns this

      this for method chaining

      Throws on error

      Use this for streaming or dynamically generated content. The read callback will be called multiple times to supply data chunks. Return null from the read callback to signal EOF.

      let currentOffset = 0
      const data = Buffer.from('Large data to stream...')

      part
      .setName('stream')
      .setDataCallback(data.length, {
      read: (size) => {
      if (currentOffset >= data.length) return null // EOF
      const chunk = data.slice(currentOffset, currentOffset + size)
      currentOffset += chunk.length
      return chunk
      },
      seek: (offset, origin) => {
      if (origin === 0) { // SEEK_SET
      currentOffset = offset
      return true
      }
      return false
      }
      })
    • Sets the data content for this MIME part from a Node.js Readable stream.

      Parameters

      • stream: Readable

        Node.js Readable stream to read data from

      • unpause: () => void

        Callback function to unpause the transfer when data becomes available

      • Optionalsize: number

        Optional expected total size in bytes (for Content-Length header)

      Returns this

      this for method chaining

      Throws if stream emits an error

      This method provides a wrapper around setDataCallback for working with Node.js streams. The stream is kept in paused mode and data is read synchronously when curl requests it. When no data is available, the transfer is paused using CurlReadFunc.Pause, and the unpause callback is invoked when data becomes available to resume the transfer.

      The unpause function should unpause the curl handle's receive operation, typically by calling handle.pause(handle.pauseFlags & ~CurlPause.Recv).

      For very large files, consider using setFileData instead, as it streams directly from disk without going through Node.js streams.

      Stream from file with unpause:

      import { createReadStream } from 'fs'
      import { CurlPause } from 'node-libcurl'

      const stream = createReadStream('/path/to/file.txt')
      const curl = new Curl()
      const mime = new CurlMime(curl)

      mime
      .addPart()
      .setName('document')
      .setDataStream(stream, () => {
      curl.pause(curl.handle.pauseFlags & ~CurlPause.Recv)
      })
      .setType('text/plain')

      Stream with known size:

      import { createReadStream, statSync } from 'fs'
      import { CurlPause } from 'node-libcurl'

      const filepath = '/path/to/file.txt'
      const size = statSync(filepath).size
      const stream = createReadStream(filepath)
      const curl = new Curl()
      const mime = new CurlMime(curl)

      mime
      .addPart()
      .setName('document')
      .setDataStream(
      stream,
      () => curl.pause(curl.handle.pauseFlags & ~CurlPause.Recv),
      size
      )
    • Sets the content transfer encoding for this part.

      Parameters

      • encoding: null | "binary" | "8bit" | "7bit" | "base64" | "quoted-printable"

        Encoding scheme: 'binary', '8bit', '7bit', 'base64', 'quoted-printable', or null to disable

      Returns this

      this for method chaining

      Throws on error

      • 'binary': No encoding, header added
      • '8bit': No encoding, header added
      • '7bit': Validates 7-bit compliance
      • 'base64': Base64 encoding
      • 'quoted-printable': Quoted-printable encoding

      Do NOT use encoding with multipart content (subparts).

      part
      .setName('data')
      .setData(binaryData)
      .setEncoder('base64')
    • Sets the data content for this MIME part by reading from a file.

      Parameters

      • filePath: null | string

        Path to the file, or null to reset

      Returns this

      this for method chaining

      Throws on error

      The file is streamed during transfer for memory efficiency. The file must be readable when the transfer starts.

      part
      .setName('document')
      .setFileData('/path/to/document.pdf')
      .setType('application/pdf')
    • Sets the remote filename for this part.

      Parameters

      • fileName: null | string

      Returns this

      this for method chaining

      Throws on error

      This appears in the Content-Disposition header and can be different from the actual local filename when using setFileData.

      part
      .setName('avatar')
      .setFileData('/tmp/temp123.jpg')
      .setFileName('profile-photo.jpg')
    • Sets custom headers for this MIME part.

      Parameters

      • headers: null | string[]

        Array of header strings, or null to reset

      Returns this

      this for method chaining

      Throws on error

      Each header should be in the format "Header-Name: value".

      part.setName('doc').setData('content').setHeaders([
      'Content-ID: <document123>',
      'X-Custom-Header: custom-value'
      ])
    • Sets the field name for this MIME part.

      Parameters

      • name: null | string

        The field name, or null to reset

      Returns this

      this for method chaining

      Throws on error

      part.setName('username').setData('john_doe')
      
    • Sets a MIME structure as nested subparts of this part.

      Parameters

      • mime: null | CurlMime

        A CurlMime instance to use as subparts, or null to reset

      Returns this

      this for method chaining

      Throws on error

      Ownership of the MIME structure transfers to this part. The subparts MIME object should not be used after this call.

      const subMime = new CurlMime(curl)
      subMime
      .addPart()
      .setName('nested_field')
      .setData('nested content')

      mime
      .addPart()
      .setName('multipart_section')
      .setSubparts(subMime)
    • Sets the content type (MIME type) for this part.

      Parameters

      • mimeType: null | string

        The MIME type string, or null to use default

      Returns this

      this for method chaining

      Throws on error

      part
      .setName('avatar')
      .setFileData('image.png')
      .setType('image/png')
    MMNEPVFCICPMFPCPTTAAATR