Skip to content

MIME and Multipart

Qriist edited this page Sep 16, 2026 · 1 revision

MIME multipart encoding is used for form uploads where each field carries its own name, content, and type. LibQurl wraps libcurl's MIME API with a focus on AttachMimePart as the primary entry point.

Creating a MIME Handle

MimeInit creates a mime_handle. Per libcurl's design, every mime_handle is associated with an easy_handle at creation time:

mime_handle := curl.MimeInit() ;default easy_handle

mime_handle2 := curl.MimeInit(easy_handle) ;specified handle

Attaching Parts

AttachMimePart is the primary method. It does three things in one call:

  1. Initializes the mime_part
  2. Assigns the field name
  3. Auto-detects the MIME type via libmagic

It returns the mime_part pointer in case you need to perform additional operations on it.

Source Types

AttachMimePart accepts all common AHKv2 source types, as demonstrated in the example assignments below.

;This is a functional sample script.
#Include <Aris\LibQurl>

curl := LibQurl()
curl.SetOpt("URL", "https://httpbin.org/anything")
mime_handle := curl.MimeInit()

; String
inStr := "abc"
curl.AttachMimePart("String", inStr)

; Integer
inInt := 123
curl.AttachMimePart("Integer", inInt)

; Object (dumped as JSON)
inObj := { a: "b" }
curl.AttachMimePart("Object", inObj)

; Map (dumped as JSON)
inMap := Map("a", "b")
curl.AttachMimePart("Map", inMap)

; Array (dumped as JSON)
inArr := ["a", "b"]
curl.AttachMimePart("Array", inArr)

; Buffer
inBuf := Buffer(100, 255)
curl.AttachMimePart("Buffer", inBuf)

; File
inFile := FileOpen(A_ScriptDir "\file.zip", "r")
curl.AttachMimePart("upload", inFile)

curl.Sync()
body := curl.GetLastBody()

msgbox curl.PrintObj(body)

String and Integer are sent as-is. Object, Array, and Map are dumped to JSON. Buffer and File are sent as binary.

Nesting MIME Handles

AttachMimeAsPart embeds an entire mime_handle as a part within another:

mime_root := curl.MimeInit()
curl.AttachMimePart("field1", "value1")
curl.AttachMimePart("field2", 42)

mime_child := curl.MimeInit()
curl.AttachMimePart("nested1", "abc")
curl.AttachMimePart("nested2", { key: "val" })

; Embed the child mime_handle as a part of the root
curl.AttachMimeAsPart("this is a nested mime", mime_child, mime_root)

curl.Sync()

Cleanup Rules

  • Only clean up root mime_handles. mime_parts are culled automatically.
  • A mime_handle that has been attached as a part via AttachMimeAsPart should not be manually cleaned up. LibQurl detects and safely ignores such handles if you attempt it.
; Ignored: this was embedded as a part, LibQurl protects it
curl.MimeCleanup(mime_child)  ; safely ignored

; Correct: clean up the root
curl.MimeCleanup(mime_root)

Where to Go Next

Clone this wiki locally