Skip to content

Commit 5134add

Browse files
author
Ong Yong Xin
authored
Add -L option to curl command
The -L (—location) flag allows redirection to another URL if the response code is 3XX, and the 'Location' header is set. The requests module enables redirection implicitly for the GET method, but the other methods (HEAD, POST, etc.) do not enable redirection.
1 parent fd3b384 commit 5134add

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

bin/curl.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
try:
1212
import clipboard
1313
except ImportError:
14-
pass
14+
clipboard = None
1515

1616

1717
def main(args):
@@ -24,6 +24,12 @@ def main(args):
2424
action='store_true',
2525
help='write output to a local file named like the remote file we get'
2626
)
27+
ap.add_argument(
28+
'-L',
29+
'--location',
30+
action='store_true',
31+
help='follow redirects to other web pages (if the URL has a 3XX response code)'
32+
)
2733
ap.add_argument(
2834
'-X',
2935
'--request-method',
@@ -46,11 +52,24 @@ def main(args):
4652
headers[name.strip()] = value.strip()
4753

4854
if ns.request_method == 'GET':
49-
r = requests.get(url, headers=headers)
55+
r = requests.get(
56+
url,
57+
headers=headers,
58+
allow_redirects=ns.location
59+
)
5060
elif ns.request_method == 'POST':
51-
r = requests.post(url, data=ns.data, headers=headers)
61+
r = requests.post(
62+
url,
63+
data=ns.data,
64+
headers=headers,
65+
allow_redirects=ns.location
66+
)
5267
elif ns.request_method == 'HEAD':
53-
r = requests.head(url, headers=headers)
68+
r = requests.head(
69+
url,
70+
headers=headers,
71+
allow_redirects=ns.location
72+
)
5473
else:
5574
print('unknown request method: {}'.format(ns.request_method))
5675
return

0 commit comments

Comments
 (0)