Skip to content

Commit

Permalink
Add --data-raw to utils.curl and fix missing method with data (#4612)
Browse files Browse the repository at this point in the history
  • Loading branch information
noviluni committed Jun 29, 2020
1 parent 0c8d8c5 commit 464f24f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 9 additions & 3 deletions scrapy/utils/curl.py
Expand Up @@ -17,8 +17,8 @@ def error(self, message):
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', default='get')
curl_parser.add_argument('-d', '--data', dest='data')
curl_parser.add_argument('-X', '--request', dest='method')
curl_parser.add_argument('-d', '--data', '--data-raw', dest='data')
curl_parser.add_argument('-u', '--user', dest='auth')


Expand Down Expand Up @@ -66,7 +66,9 @@ def curl_to_request_kwargs(curl_command, ignore_unknown_options=True):
if not parsed_url.scheme:
url = 'http://' + url

result = {'method': parsed_args.method.upper(), 'url': url}
method = parsed_args.method or 'GET'

result = {'method': method.upper(), 'url': url}

headers = []
cookies = {}
Expand All @@ -90,5 +92,9 @@ def curl_to_request_kwargs(curl_command, ignore_unknown_options=True):
result['cookies'] = cookies
if parsed_args.data:
result['body'] = parsed_args.data
if not parsed_args.method:
# if the "data" is specified but the "method" is not specified,
# the default method is 'POST'
result['method'] = 'POST'

return result
25 changes: 25 additions & 0 deletions tests/test_utils_curl.py
Expand Up @@ -141,6 +141,31 @@ def test_post(self):
}
self._test_command(curl_command, expected_result)

def test_post_data_raw(self):
curl_command = (
"curl 'https://www.example.org/' --data-raw 'excerptLength=200&ena"
"bleDidYouMean=true&sortCriteria=ffirstz32xnamez32x201740686%20asc"
"ending&queryFunctions=%5B%5D&rankingFunctions=%5B%5D'"
)
expected_result = {
"method": "POST",
"url": "https://www.example.org/",
"body": (
"excerptLength=200&enableDidYouMean=true&sortCriteria=ffirstz3"
"2xnamez32x201740686%20ascending&queryFunctions=%5B%5D&ranking"
"Functions=%5B%5D")
}
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 = {
"method": "GET",
"url": "http://httpbin.org/anything",
"body": "asdf"
}
self._test_command(curl_command, expected_result)

def test_patch(self):
curl_command = (
'curl "https://example.com/api/fake" -u "username:password" -H "Ac'
Expand Down

0 comments on commit 464f24f

Please sign in to comment.