1

I am trying to post data to API. I am building request every time. and I am adding headers to wrk.format() method. The headers are not accepting event though headers type is string.

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

throwing error :

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

Can anyone help me with this?

Thanks in advance.

Son Truong
  • 13,661
  • 5
  • 32
  • 58
shakeel
  • 801
  • 1
  • 8
  • 24
  • Third argument must be a table `headers`. You've passed a string `s[1]` instead. Correct example: `wrk.format("POST", "/api/", {["Authorization"] = "key"} ,data)` – Egor Skriptunoff Aug 28 '18 at 16:51
  • if i use above syntax. even it is throwing error. bad argument #1 to 'len' (string expected, got table)) – shakeel Aug 28 '18 at 16:59
  • Fourth argument to `wrk.format(.., .., .., body)` must be a string. Probably your `data` is a table. – Egor Skriptunoff Aug 29 '18 at 09:17

1 Answers1

1

You need to pass the entire dictionary to the format function

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

in case of mine the headers look like:

headers = {}
headers["Content-Type"] = "application/json"
s510
  • 2,271
  • 11
  • 18