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

时间:2018-05-04 05:53:36

标签: json lua multipartform-data wrk

我正在尝试使用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--
如果我在代码中读取上面的请求块作为jsonBody变量并将其作为body参数传递给wrk.format函数,那么lua中的wrk_script下面的

不起作用。

-- Load data from the file
function load_data_from_file(file)
  lines = {}

  -- Check if the file exists
  local f=io.open(file,"r")
  if f~=nil then
    io.close(f)
  else
    -- Return the empty array
    return lines
  end

  -- If the file exists loop through all its lines
  -- and add them into the lines array
  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

-- Load data from file, here testdata.csv, should be in this directory
dataSets = load_data_from_file("testdata.csv")
if #dataSets == 0 then
  print("Not a valid input data file. [testdata.csv]")
  return
end

-- Initialize the dataSets array iterator
counter = 0

-- request handler
request = function()
  counter = counter + 1
  local req = nil
  if dataSets[counter] ~= nil then
    -- Get the next dataSets array element
    url_path = "/person/" .. dataSets[counter].person .. "/names"
    req = wrk.format(nil,  url_path, nil, dataSets[counter].jsonBody )
  end
  -- If the counter is longer than the dataSets array length then reset it
  if counter > #dataSets then
    counter = 0
  end
  -- Return the request object with the current URL path
  return req
end

-- response handler
response = function(status, headers, body)
end

寻找专家意见

0 个答案:

没有答案