Using Apache Bench (ab) to POST JSON to an API

Apache bench is a popular load testing application. But how do you use it to test an API that accepts a JSON payload using the POST method?

You can use the following command:

ab -p json.txt -T application/json -H 'Authorization: Token abcd1234' -c 10 -n 2000 http://example.com/json/api
  • json.txt contains the json you want to post
  • -p means to POST it
  • -H adds an Auth header (could be Basic or Token)
  • -T sets the Content-Type
  • -c is concurrent clients
  • -n is the number of requests to run in the test

You can try adding the -l key to accept dynamic response length if there are lots of failed requests.