Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a POST request using ab (apache benchmarking) on a django server

Tags:

I'm trying to make a HTTP POST request using ab to a form built with django.

I'm using the following line:

ab -n 10 -C csrftoken=my_token -p ab_file.data -T application/x-www-form-urlencoded http://localhost:8000/

My ab_file.data looks like this:

url=my_encoded_url&csrfmiddlewaretoken=my_token

It always returns a 403 status code.

When I use curl using the same parameters, it works. The curl line:

curl -X POST -d "url=my_encoded_url&csrfmiddlewaretoken=my_token" --cookie "csrftoken=my_token" http://localhost:8000/

How can I do that?

like image 831
hermancaldara Avatar asked Apr 19 '15 14:04

hermancaldara


People also ask

How do I use the ab Apache HTTP server benchmarking tool?

Apache Bench (ab) is a load testing and benchmarking tool for Hypertext Transfer Protocol (HTTP) server. It can be run from command line and it is very simple to use. A quick load testing output can be obtained in just one minute.

What is Apache benchmark?

ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.

What utility can be used to test Apache performance?

Apache Bench or ab for short, is a command-line tool to perform simple load tests on an HTTP server, be it a website or an API.


2 Answers

File must have a properly url-encode data. If you url-encode manually, it is too easy to have typos like blanks wrong encodes. Best do it programmatically. See an another answer: Apache Bench and POST data on how to use Python to create such file ( ex: post.data)

Then use: ab -T 'application/x-www-form-urlencoded' -n 10 -p post.data http://localhost:8080/

like image 180
jacobm654321 Avatar answered Sep 20 '22 19:09

jacobm654321


When using ab, the entire contents of the data file must be wrapped onto a single line - it fails silently if it's normally expanded JSON. So a post from a data file that works fine with curl will fail with ab until you do this.

Tip: If using Atom or VSCode, select all and hit Cmd-J to wrap everything to one line.

like image 30
shacker Avatar answered Sep 19 '22 19:09

shacker