wrk工具- 如何将multipart/form-data传递给wrk.format函数

我正在尝试使用wrk进行https请求生成。我该如何将multipart/form-data请求传递给wrk.format函数呢?我的请求形式如下:

Accept: application/json
Content-Type: multipart/form-data; boundary="----=_Part_0_140715475.1525373091109"
MIME-Version: 1.0

------=_Part_0_140715475.1525373091109
Content-Type: application/json; name=PersonName.json
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="PersonName"; filename="PersonName.json"

{
   "Person":{
      "Name":"test2",
      "age":"23"
   }
}
------=_Part_0_140715475.1525373091109--

在lua中的以下wrk脚本无法工作,如果我将上面的请求块作为jsonBody变量读取到代码中并将其作为body参数传递给wrk.format函数。

-- 从文件中读取数据
function load_data_from_file(file)
  lines = {}

  -- 检查文件是否存在
  local f=io.open(file,"r")
  if f~=nil then
    io.close(f)
  else
    -- 返回空数组
    return lines
  end

  -- 如果文件存在,循环其所有行,并将其添加到lines数组中
  for line in io.lines(file) do
    if not (line == '') then
      for a, b in line:gmatch( "([^,]+),([^,]+)" ) do
        lines[#lines + 1] = { person = a, jsonBody = b }
      end
    end
  end

  return lines
end

function init(args)
   requests  = 0
   responses = 0
   wrk.method = '_REQUESTMETHOD_'
   wrk.body   = '_REQUESTBODY_'
   _HEAD_
end

-- 从文件中加载数据,这里的testdata.csv应该位于此目录中
dataSets = load_data_from_file("testdata.csv")
if #dataSets == 0 then
  print("无效的输入数据文件。[testdata.csv]")
  return
end

-- 初始化dataSets数组迭代器
counter = 0

-- 请求处理程序
request = function()
  counter = counter + 1
  local req = nil
  if dataSets[counter] ~= nil then
    -- 获取下一个dataSets数组元素
    url_path = "/person/" .. dataSets[counter].person .. "/names"
    req = wrk.format(nil,  url_path, nil, dataSets[counter].jsonBody )
  end
  -- 如果计数器大于dataSets数组长度,则重置
  if counter > #dataSets then
    counter = 0
  end
  -- 返回当前URL路径的请求对象
  return req
end

-- 响应处理程序
response = function(status, headers, body)
end

寻求专家建议

点赞