POST request with wrk?
Asked Answered
M

4

41

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

Martineau answered 7/3, 2013 at 1:47 Comment(0)
A
89

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.

Aggregate answered 12/11, 2014 at 20:57 Comment(2)
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.Martineau
You sir, are a life saver ! Just what I wanted. A simple and elegant way to test my post APIs. +1Supraliminal
M
41

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"
Morvin answered 31/1, 2019 at 9:36 Comment(1)
To post json data, this formatting actually works for me: wrk.body = '{"firstKey": "somedata", "secondKey": "somedata"}'Bareback
I
15

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

Indian answered 30/3, 2020 at 8:34 Comment(1)
Just what I was looking for!Keg
T
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
Tidbit answered 24/5, 2019 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.