-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
541017d
0ee96a5
513cb46
1588a79
3e7fe23
8633569
51bd31a
fedcf9e
a84db7e
8a23a08
b38e3fd
4a19a63
3e46a90
b57d2ff
51bf1e4
117b669
3a47469
4c445cf
158f7a3
0a8ebf4
e104658
1cc478d
aae1b4f
868162e
2a9c604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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' } | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: keep a space before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
|
@@ -213,6 +213,7 @@ local function choose_boundary() | |
return str_sub(tostring({}), 10) | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok