Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Copy to cURL" in Chrome to Apache Bench command
#!/usr/bin/env python3
import sys
import os
def curl_to_ab(curl_cmd: list, num: int=200, cur: int=4) -> str:
"""
Translate a cURL command created by Chrome's developer tools into a
command for ``ab``, the ApacheBench HTTP benchmarking tool.
Parameters
----------
curl_cmd
The string given to you by the "Copy to cURL" context action in
Chrome's developer tools.
num : int
The number of requests ApacheBench should make in total
cur : int
The number of concurrent requests ApacheBench should make
Note
----
Not all headers play nice with ApacheBench, so ``headers_to_copy`` has
been set to a reasonable default. Tweak this if you need something
else in there.
"""
url = curl_cmd[1]
headers_to_copy = [
'Origin',
'Authorization',
'Accept'
]
headers = []
for i, part in enumerate(curl_cmd):
if part == '-H':
header = curl_cmd[i+1]
if any([h in header for h in headers_to_copy]):
headers.append("'{}'".format(header))
cmd = ['ab -n {} -c {}'.format(num, cur)]
cmd += ['-H {}'.format(part) for part in headers]
cmd.append("'{}'".format(url))
return ' '.join(cmd)
if __name__ == '__main__':
"""Usage: python curl_to_ab.py <cURL command from Chrome>
"""
os.system(curl_to_ab(sys.argv[1:]))
@bweston92

This comment has been minimized.

Copy link

@bweston92 bweston92 commented Dec 12, 2018

not all heros wear capes!

@CaseyPeters

This comment has been minimized.

Copy link

@CaseyPeters CaseyPeters commented Dec 14, 2018

I wasn't expecting google to find something this perfect ❤️

@etagwerker

This comment has been minimized.

Copy link

@etagwerker etagwerker commented Jul 21, 2019

@ctolsen Thanks for sharing! I had to slightly tweak it so that it used my current session (I was testing an authenticated URL)

https://gist.github.com/etagwerker/eeb54fa1fdcd8ea6d5b9b168fdfa9952

@pelletencate

This comment has been minimized.

Copy link

@pelletencate pelletencate commented Dec 11, 2019

I wasn't expecting google to find something this perfect ❤️

Neither did I. I guess @ctolson and @google got together earlier and figured that out?

🥇

@ahivert

This comment has been minimized.

Copy link

@ahivert ahivert commented Apr 30, 2020

Helped me a lot so I published it online to be more easy to use.
-> https://curl2ab.ahivert.dev

@pokrovskyy

This comment has been minimized.

Copy link

@pokrovskyy pokrovskyy commented Jun 10, 2020

@ahivert, that is awesome, but please update the output command to take the URL in quotes. Otherwise it blows up in the command line, especially for URLs with parameters like http://foo.bar/path?param1=value2&param2=value2

@ahivert

This comment has been minimized.

Copy link

@ahivert ahivert commented Jun 11, 2020

Thanks @pokrovskyy I fixed it

@sogouo

This comment has been minimized.

Copy link

@sogouo sogouo commented Nov 30, 2020

@ctolsen Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.