Run the POST request using ab (apache benchmarking) on ​​the django server

I am trying to execute an HTTP POST request using ab in a form built using django.

I am 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 as follows:

 url=my_encoded_url&csrfmiddlewaretoken=my_token 

It always returns a 403 status code.

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

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

How can i do this?

+11
post django apachebench
source share
3 answers

@ jacobm654321,

for sure, the best thing to do is encode the URL programmatically. But that was not my problem. My problem is that the file containing the post data had an empty line at the end of the file. EditorConfig put it there. After deleting this empty line, everything worked.

Thanks anyway.

+2
source share

The file must have the correct url encoded data. If you url-encode manually, it is too easy to have typos like spaces, wrong encodings. This is best done programmatically. See Another Answer: Apache Bench and POST data on how to use Python to create such a file (ex: post.data)

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

+14
source share

When using ab, the entire contents of the data file must be enclosed in one line - it silently fails if it usually extends JSON. So a message from a data file that works fine with curl will not work with ab until you do this.

Tip: If you are using Atom or VSCode, select everything and press Cmd-J to wrap everything on one line.

+6
source share

All Articles