diff --git a/scrapy/utils/curl.py b/scrapy/utils/curl.py index a2243ae2e..5e095f933 100644 --- a/scrapy/utils/curl.py +++ b/scrapy/utils/curl.py @@ -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")