39

I started to try wrk. It's very simple to use and very hard on the server, but I don't know how to perform other kind of request such as POST. In fact, I don't even know if this tool allows it. The documentation is very minimal.

Thanks

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
jackdbernier
  • 1,542
  • 1
  • 17
  • 32

4 Answers4

84

This is possible now. Here is an example https://github.com/wg/wrk/blob/master/scripts/post.lua.

wrk.method = "POST"
wrk.body   = "foo=bar&baz=quux"
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"

save this in a *.lua script and pass it into your command line test with the -s flag.

Steve
  • 25,806
  • 2
  • 33
  • 43
  • 1
    wow! Let me check that out. I'll switch the answer to your's if I can manage to make it work. Thanks for coming back to that post. – jackdbernier Nov 13 '14 at 06:09
  • 1
    You sir, are a life saver ! Just what I wanted. A simple and elegant way to test my post APIs. +1 – Saif Asif Jan 31 '16 at 06:16
36

for those looking for a content-type "application/json" example:

wrk.method = "POST"
wrk.body = '{"firstKey": "somedata", "secondKey": "somedata"}'
wrk.headers["Content-Type"] = "application/json"
Vlad Frolov
  • 7,445
  • 5
  • 33
  • 52
Emmanuel N K
  • 8,710
  • 1
  • 31
  • 37
  • 7
    To post json data, this formatting actually works for me: `wrk.body = '{"firstKey": "somedata", "secondKey": "somedata"}'` – suvtfopw Jun 04 '19 at 10:37
13

Here's an example lua script post_binary.lua for posting binary file: "Content-Type:application/octet-stream".

wrk.method = "POST"
wrk.headers["Content-Type"] = "application/octet-stream"

file = io.open("dog.jpg", "rb")
wrk.body = file:read("*a")

Then try: wrk "your_url" -s post_binary.lua --latency -t 1 -c 1 -d 30s -R 1

hao
  • 1,144
  • 12
  • 15
12

I'll recommend using wrk2 instead of wrk since wrk2 provides better support for concurrent requests. When content-type header is application/json then please escape the special characters like \n with \\n and all other special characters present. Not doing this will send an invalid json to the upstream API, which will waste your time debugging.

Create a file with extension lua and paste the following in it. Save it and pass it along with -s flag to wrk2 command.

wrk.method = "POST"
wrk.body = "{\"firstKey\": 'somedata', \"secondKey\": 'somedata'}"
wrk.headers["Content-Type"] = "application/json"

Also you can add multiple header as

wrk.headers["Header1"] = "Header1_Val"
wrk.headers["Header2"] = "Header2_Val"
wrk.headers["Header3"] = "Header3_Val"
wrk.headers["Header4"] = "Header4_Val"
wrk2 -t500 -c1000 -d160s -R10000 -s ~/Documents/luaTestScript.lua http://localhost:8080/test_endpoint