Last active
July 29, 2024 14:19
-
-
Save ctolsen/5679e874719de7500a0b to your computer and use it in GitHub Desktop.
"Copy to cURL" in Chrome to Apache Bench command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:])) | |
@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¶m2=value2
Thanks @pokrovskyy I fixed it
@ctolsen Thanks for sharing!
@ahivert it doesnt work with POST requests.
❤️ it, thanks!
Forked to add support for POST data using a temp file (Chrome uses --data-binary
and --data-raw
for POST): https://gist.github.com/odony/93086f5c917e777cb2f4d0c04d477545
@ahivert
https://curl2ab.ahivert.dev/ has erorr and does not working
src.e406c71d.js:5 Uncaught ReferenceError: curlInpu is not defined
Also, i forkd repo and fixed error. Now, curl2ab available on https://kaspiman.github.io/curl2ab/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helped me a lot so I published it online to be more easy to use.
-> https://curl2ab.ahivert.dev