Repository navigation
MIME and Multipart
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.
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 handleAttachMimePart is the primary method. It does three things in one call:
- Initializes the
mime_part - Assigns the field name
- Auto-detects the MIME type via libmagic
It returns the mime_part pointer in case you need to perform additional operations on it.
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.
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()- Only clean up root
mime_handles.mime_parts are culled automatically. - A
mime_handlethat has been attached as a part viaAttachMimeAsPartshould 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)- POST and Upload — simpler single-body uploads
-
MIME Handle Reference — full method list including individual
MimePart*methods
Foundations
Operations
- Download Modes
- Setting and Listing Options
- Setting and Inspecting Headers
- POST and Upload
- Multiple Easy Handles
- Async and Multi Interface
- URL Construction
- Share Interface
- MIME and Multipart
- Raw Send and Receive
- Pause and Resume
- WebSocket
- SSL Sessions
- Debug and Diagnostics
API Reference
- Easy Handle Reference
- Multi Handle Reference
- Share Handle Reference
- MIME Handle Reference
- URL Handle Reference
- Utility Methods Reference
- WebSocket Reference
Code Snippets