Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip_headers argument #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion curlify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
from pipes import quote


def to_curl(request, compressed=False, verify=True):
def to_curl(request, compressed=False, verify=True, skip_headers=False):
"""
Returns string with curl command by provided request object

Parameters
----------
compressed : bool
If `True` then `--compressed` argument will be added to result
verify: bool
If `True` then `--insecure` argument will be added to result
skip_headers: bool
If 'True' then headers [Accept, Accept-Encoding, Connection, User-Agent, Content-Length] will be skipped
"""
parts = [
('curl', None),
('-X', request.method),
]

sys_headers = ['accept', 'accept-encoding', 'connection', 'user-agent', 'content-length']
for k, v in sorted(request.headers.items()):
if skip_headers and k.lower() in sys_headers:
continue
parts += [('-H', '{0}: {1}'.format(k, v))]

if request.body:
Expand Down
14 changes: 14 additions & 0 deletions curlify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,17 @@ def test_post_csv_file():
)

assert curlified == expected


def test_skip_headers():
r = requests.get(
"http://google.ru",
data={"a": "b"},
cookies={"foo": "bar"},
)
assert curlify.to_curl(r.request, skip_headers=True) == (
"curl -X GET "
"-H 'Content-Type: application/x-www-form-urlencoded' "
"-H 'Cookie: foo=bar' "
"-d a=b http://google.ru/"
)