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.
Signature
Section titled “Signature”http_multipart(url, fields, [headers])
asyncParameters
Section titled “Parameters”| 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 nametype(optional) - MIME type (e.g."image/png")
Returns
Section titled “Returns”Same as http_get.
Example
Section titled “Example”-- Text fields onlylocal res, err = http_multipart("https://api.example.com/form", { name = "SpyWeb", version = "1.0"})if not res then log("upload failed: " .. err.error) returnend
-- 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) returnendSee Also
Section titled “See Also”- http_get - HTTP GET request
- http_request - Arbitrary HTTP request with proxy support
- fs_read_binary - Read binary files