Skip to content

Commit 7c28904

Browse files
committed
Add. Mime HTTP example.
1 parent cacdb9c commit 7c28904

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

examples/cURLv3/post_mime.lua

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local curl = require "cURL"
2+
3+
local null = curl.null
4+
5+
-- make first request
6+
7+
-- Documentation does not restrict use same mime with different easy handle.
8+
-- In fact it does not mention this case at all. But in my experiments it works.
9+
local easy = curl.easy()
10+
11+
-- Create new mime object.
12+
-- cURL documentation doesn not mentioned that
13+
-- this mime object can be used only
14+
local mime = easy:mime()
15+
16+
-- Add part. Source of data is file on disk.
17+
-- If file not exists then perform returns `[CURL-EASY][READ_ERROR]` error
18+
-- There no way to remove existed part from mime.
19+
local part = mime:addpart{
20+
filedata = './post_mime.lua',
21+
type = 'application/lua',
22+
filename = 'post_mime.lua',
23+
encoder = 'base64',
24+
}
25+
26+
local buffer = {}
27+
28+
-- Use mime object as request body
29+
easy:setopt{
30+
url = 'http://127.0.0.1:7090/post',
31+
mimepost = mime,
32+
}
33+
34+
easy:setopt_writefunction(table.insert, buffer)
35+
36+
local ok, err = easy:perform()
37+
if ok then
38+
local code, url, content = easy:getinfo_effective_url(),
39+
easy:getinfo_response_code(), table.concat(buffer)
40+
print(code, url)
41+
print(content)
42+
else
43+
print(err)
44+
end
45+
46+
easy:close()
47+
48+
-- E.g. it does not send filedata second time.
49+
-- Lua-cURL does not free mime when close `parent` easy.
50+
51+
-- Explicitly free mime object and all its parts
52+
-- There no way to reuse only some parts.
53+
mime:free()
54+
55+
-- Subparts become also freed
56+
print(part)

0 commit comments

Comments
 (0)