Headers are not accepting in wrk.format

尝试向API提交数据。每次构建请求并将头添加到wrk.format()方法中。即使头类型为字符串也无法接受头。

headers = {}
s = {}
request = function()

 headers["Authorization"] = "key"
 for name, value in pairs(headers) do
      s[1] = string.format("%s: %s", name, value)
 end
 print(s[1])
 print(type(s[1])
 return wrk.format("POST", "/api/", s[1] ,data)
end

抛出错误 :

PANIC: unprotected error in call to Lua API ([string "wrk"]:0: attempt to index field 'headers' (a string value))

有人能帮我吗?

提前致谢。

点赞
用户12775531
用户12775531

你需要将整个字典传递给 format 函数:

return wrk.format("POST", "/api/", headers, data)

例如,我的头部信息如下所示:

headers = {}
headers["Content-Type"] = "application/json"
2022-10-26 12:54:21