mirror of https://github.com/scrapy/scrapy.git
Add Request.to_curl() as the inverse of Request.from_curl()
Closes #7743. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
870803b7fb
commit
5a9a414d82
|
|
@ -257,6 +257,8 @@ Request objects
|
||||||
|
|
||||||
.. automethod:: from_curl
|
.. automethod:: from_curl
|
||||||
|
|
||||||
|
.. automethod:: to_curl
|
||||||
|
|
||||||
.. automethod:: to_dict
|
.. automethod:: to_dict
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -381,6 +381,16 @@ class Request(object_ref):
|
||||||
request_kwargs.update(kwargs)
|
request_kwargs.update(kwargs)
|
||||||
return cls(**request_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]:
|
def to_dict(self, *, spider: scrapy.Spider | None = None) -> dict[str, Any]:
|
||||||
"""Return a dictionary containing the Request's data.
|
"""Return a dictionary containing the Request's data.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -406,6 +406,19 @@ class TestRequest:
|
||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(RuntimeError):
|
||||||
NO_CALLBACK()
|
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):
|
def test_from_curl(self):
|
||||||
# Note: more curated tests regarding curl conversion are in
|
# Note: more curated tests regarding curl conversion are in
|
||||||
# `test_utils_curl.py`
|
# `test_utils_curl.py`
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue