Skip to content

http_multipart

http_multipart(url, fields, [headers]) performs a multipart/form-data POST request for file uploads. Returns (response, nil) on success or (nil, error_table) on failure.

http_multipart(url, fields, [headers])
async
Param Type Description
url string Target URL
fields table Form fields (see below)
headers table? Optional key-value header pairs

Fields table:

Each value is either:

  • A string - sent as a text form field
  • A table - sent as a file attachment with:
    • content (required) - file bytes (binary-safe Lua string)
    • filename (optional) - server-side file name
    • type (optional) - MIME type (e.g. "image/png")

Same as http_get.

-- Text fields only
local res, err = http_multipart("https://api.example.com/form", {
name = "SpyWeb",
version = "1.0"
})
if not res then
log("upload failed: " .. err.error)
return
end
-- File upload (screenshot)
local page = cdp.launch({ headless = true }):attach()
page:open("https://example.com")
page:screenshot("page.png", { full_page = true })
local image_data = fs_read_binary("page.png")
local res, err = http_multipart("https://hooks.example.com/upload", {
screenshot = { content = image_data, filename = "page.png", type = "image/png" },
caption = "my screenshot"
})
if not res then
log("screenshot upload failed: " .. err.kind)
return
end