Merge pull request #5901 from jxlil/fix/5899

Fix: Request.from_curl() with $-prefixed string literals
This commit is contained in:
Andrey Rakhmatullin 2023-04-19 13:35:01 +04:00 committed by GitHub
commit abc9c1ac6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -7,6 +7,14 @@ from urllib.parse import urlparse
from w3lib.http import basic_auth_header
class DataAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
value = str(values)
if value.startswith("$"):
value = value[1:]
setattr(namespace, self.dest, value)
class CurlParser(argparse.ArgumentParser):
def error(self, message):
error_msg = f"There was an error parsing the curl command: {message}"
@ -17,7 +25,7 @@ curl_parser = CurlParser()
curl_parser.add_argument("url")
curl_parser.add_argument("-H", "--header", dest="headers", action="append")
curl_parser.add_argument("-X", "--request", dest="method")
curl_parser.add_argument("-d", "--data", "--data-raw", dest="data")
curl_parser.add_argument("-d", "--data", "--data-raw", dest="data", action=DataAction)
curl_parser.add_argument("-u", "--user", dest="auth")

View File

@ -154,6 +154,15 @@ class CurlToRequestKwargsTest(unittest.TestCase):
}
self._test_command(curl_command, expected_result)
def test_post_data_raw_with_string_prefix(self):
curl_command = "curl 'https://www.example.org/' --data-raw $'{\"$filters\":\"Filter\u0021\"}'"
expected_result = {
"method": "POST",
"url": "https://www.example.org/",
"body": '{"$filters":"Filter!"}',
}
self._test_command(curl_command, expected_result)
def test_explicit_get_with_data(self):
curl_command = "curl httpbin.org/anything -X GET --data asdf"
expected_result = {