This commit is contained in:
Rishikeshan 2026-07-17 14:06:56 +05:30 committed by GitHub
commit b7e60c3365
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View File

@ -257,6 +257,8 @@ Request objects
.. automethod:: from_curl
.. automethod:: to_curl
.. automethod:: to_dict

View File

@ -381,6 +381,16 @@ class Request(object_ref):
request_kwargs.update(kwargs)
return cls(**request_kwargs)
def to_curl(self) -> str:
"""Return a string with a cURL command equivalent to this request.
Inverse of :meth:`from_curl`. See also
:func:`scrapy.utils.request.request_to_curl`.
"""
from scrapy.utils.request import request_to_curl
return request_to_curl(self)
def to_dict(self, *, spider: scrapy.Spider | None = None) -> dict[str, Any]:
"""Return a dictionary containing the Request's data.

View File

@ -406,6 +406,19 @@ class TestRequest:
with pytest.raises(RuntimeError):
NO_CALLBACK()
def test_to_curl(self):
request = self.request_class(
"https://www.httpbin.org/post",
method="POST",
headers={"Content-Type": "application/json"},
body='{"foo": "bar"}',
)
assert request.to_curl() == (
"curl -X POST https://www.httpbin.org/post"
' --data-raw \'{"foo": "bar"}\''
" -H 'Content-Type: application/json'"
)
def test_from_curl(self):
# Note: more curated tests regarding curl conversion are in
# `test_utils_curl.py`