fix: Request.from_curl() with prefixed string literals

This commit is contained in:
Jalil SA 2023-04-18 21:31:51 -05:00
parent 02f3e8d413
commit f5447f3b4c
1 changed files with 11 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import argparse
import re
import warnings
from http.cookies import SimpleCookie
from shlex import split
@ -7,6 +8,15 @@ 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).encode("utf-8").decode("utf-8")
if items := re.findall(r"{.+}", value):
value = items[0]
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 +27,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")