Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multipart/form #32

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
541017d
support set multipart/form file
zhucebuliaopx Mar 22, 2020
0ee96a5
fix add content-length
zhucebuliaopx Mar 22, 2020
513cb46
update README
zhucebuliaopx Mar 22, 2020
1588a79
Merge tag '0.0.1' into develop
zhucebuliaopx Mar 22, 2020
3e7fe23
Merge branch 'release/0.0.1'
zhucebuliaopx Mar 22, 2020
8633569
local var
zhucebuliaopx Mar 22, 2020
51bd31a
fix code style
zhucebuliaopx Mar 23, 2020
fedcf9e
Merge tag '0.0.2' into develop
zhucebuliaopx Mar 23, 2020
a84db7e
Merge branch 'release/0.0.2'
zhucebuliaopx Mar 23, 2020
8a23a08
remove git address,once upstream merage my pr,so could install lua-mu…
zhucebuliaopx Mar 23, 2020
b38e3fd
Merge tag '0.0.3' into develop
zhucebuliaopx Mar 23, 2020
4a19a63
Merge branch 'release/0.0.3'
zhucebuliaopx Mar 23, 2020
3e46a90
add test example and fix code style
zhucebuliaopx Mar 24, 2020
b57d2ff
Merge branch 'release/0.0.4'
zhucebuliaopx Mar 24, 2020
51bf1e4
Merge tag '0.0.4' into develop
zhucebuliaopx Mar 24, 2020
117b669
del unnecessary local var
zhucebuliaopx Mar 24, 2020
3a47469
fix code style
zhucebuliaopx Apr 8, 2020
4c445cf
update multipart code
zhucebuliaopx May 1, 2020
158f7a3
fix code style
zhucebuliaopx May 1, 2020
0a8ebf4
fix code style && update test case
zhucebuliaopx May 2, 2020
e104658
Merge branch 'feature/0.0.2' into develop
zhucebuliaopx May 2, 2020
1cc478d
remove dependencies by Kong\lua-multipart && update readme
zhucebuliaopx May 2, 2020
aae1b4f
fix files body with userdata fp
zhucebuliaopx May 2, 2020
868162e
fix iter_base_func is_array
zhucebuliaopx May 2, 2020
2a9c604
local var error
zhucebuliaopx May 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/resty/requests/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,24 @@ local function prepare(url_parts, session, config)
local content
local json = config.json
local body = config.body

local files = config.files

if json then
content = cjson.encode(json)
headers["content-length"] = #content
headers["content-type"] = "application/json"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to leave an empty line here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


elseif files and is_tab(files) then
local content_type = headers["content-type"]
if not content_type then
content_type = "multipart/form-data; boundary="..util.choose_boundary()
content_type = "multipart/form-data; boundary=" .. util.choose_boundary()
end
local multipart_body= util.make_multipart_body(files, content_type)
local multipart_body = util.make_multipart_body(files, content_type)
headers["content-type"] = content_type
headers["content-length"] = #multipart_body
content = multipart_body
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, leave an empty line here.


else
content = body
if is_func(body) then
Expand Down
19 changes: 10 additions & 9 deletions lib/resty/requests/util.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
-- Copyright (C) Alex Zhang

local multipart = require("resty.requests.multipart")

local type = type
local pcall = pcall
local pairs = pairs
local error = error
local rawget = rawget
local ipairs = ipairs
local require = require
local setmetatable = setmetatable
local lower = string.lower
local str_sub = string.sub
local tostring = tostring
local ngx_gsub = ngx.re.gsub
local base64 = ngx.encode_base64
local Multipart = require("resty.requests.multipart")

local _M = { _VERSION = '0.1' }

Expand Down Expand Up @@ -94,7 +94,7 @@ end

local function set_config(opts)
opts = opts or {}
local config = new_tab(0, 14)
local config = new_tab(0, 15)

-- 1) timeouts
local timeouts = opts.timeouts
Expand Down Expand Up @@ -136,9 +136,6 @@ local function set_config(opts)
-- 4) body
config.body = opts.body

-- 4.1) files
config.files = opts.files

-- 5) ssl verify
config.ssl = opts.ssl

Expand Down Expand Up @@ -196,14 +193,17 @@ local function set_config(opts)
-- 14) use_default_type
config.use_default_type = opts.use_default_type ~= false

-- 15) files
config.files = opts.files

return config
end


local function make_multipart_body(files, content_type)
local m = Multipart("", content_type)
for _, v in ipairs(files) do
m:set_simple(v[1], v[2], v[3], v[4])
local m = multipart("", content_type)
for i=1,#files do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: keep a space before #files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pytpeng
Could you add some test cases to cover this feature?

yeah

m:set_simple(files[i][1], files[i][2], files[i][3], files[i][4])
end
zhucebuliaopx marked this conversation as resolved.
Show resolved Hide resolved
return m:tostring()
end
Expand All @@ -213,6 +213,7 @@ local function choose_boundary()
return str_sub(tostring({}), 10)
end

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two empty lines are needed to separate functions.


_M.new_tab = new_tab
_M.is_str = is_str
_M.is_num = is_num
Expand Down