Apache Bench (AB) POST request with data

Learn how to use ab to test POST and PUT endpoints performance.
Published 2023-03-172 min read
Apache Bench (AB) POST request with data

AB is a simple and useful tool for benchmarking web applications. It’s pretty straightforward when it comes to benchmarking GET requests, but it’s getting more tricky for POST and PUT requests.

Apache Bench and POST requests

The first step is to save the POST body to a text file. If you have your body as a single-line string it’s enough to do something like this:

echo "your data" > file

Creating this file manually and copy-pasting the contents is fine.

Now it’s a matter of assigning correct options:

-T - application/json would be the case for most APIs

-p - path to a file with a request body

-l - accept variable length response, otherwise request with a content length different than the first one will be counted as failed

-c, -n - usual stuff, concurrency level, and number of requests.

Whole command example:

ab -T application/json -p file -l -n 1000 "http://localhost:3000/api/graphql"

Caveat: You may be tempted to set up -m option to POST, to indicate HTTP method, but -p option already sets it up for you. So if you use both -p and -m, then you’ll receive an error Cannot mix POST with other methods

Apache Bench and PUT requests

Interestingly PUT uses different flags:

-u - path to a file with a request body

other options are the same.


    ab -T application/json -u file -l -n 1000 "http://localhost:3000/api/deliveries/123"

The same caveat applies as in a previous example.

So that’s how you test the performance with ab for POST and PUT requests. Now you can test your REST API write speed or GraphQL. By the way, you can check out a short article on the best alternatives to ab..

Tags: #cli #ab