Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multipart/form-data to FormRequest #6332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/topics/request-response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ fields with form data from :class:`Response` objects.

.. class:: scrapy.http.request.form.FormRequest
.. class:: scrapy.http.FormRequest
.. class:: scrapy.FormRequest(url, [formdata, ...])
.. class:: scrapy.FormRequest(url, [formdata, is_multipart, ...])

The :class:`FormRequest` class adds a new keyword parameter to the ``__init__`` method. The
remaining arguments are the same as for the :class:`Request` class and are
Expand All @@ -810,10 +810,14 @@ fields with form data from :class:`Response` objects.
body of the request.
:type formdata: dict or collections.abc.Iterable

:param is_multipart: is a boolean flag to determine if the ``Content-Type`` should be sent
as ``multipart/form-data``.
:type is_multipart: bool

The :class:`FormRequest` objects support the following class method in
addition to the standard :class:`Request` methods:

.. classmethod:: FormRequest.from_response(response, [formname=None, formid=None, formnumber=0, formdata=None, formxpath=None, formcss=None, clickdata=None, dont_click=False, ...])
.. classmethod:: FormRequest.from_response(response, [formname=None, formid=None, formnumber=0, formdata=None, formxpath=None, formcss=None, is_multipart=False, clickdata=None, dont_click=False, ...])

Returns a new :class:`FormRequest` object with its form field values
pre-populated with those found in the HTML ``<form>`` element contained
Expand Down Expand Up @@ -862,6 +866,10 @@ fields with form data from :class:`Response` objects.
request, even if it was present in the response ``<form>`` element.
:type formdata: dict

:param is_multipart: if True, the form will be sent using the header
``Content-Type: multipart/form-data``.
:type is_multipart: bool

:param clickdata: attributes to lookup the control clicked. If it's not
given, the form data will be submitted simulating a click on the
first clickable element. In addition to html attributes, the control
Expand Down
13 changes: 11 additions & 2 deletions scrapy/http/request/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class FormRequest(Request):
valid_form_methods = ["GET", "POST"]

def __init__(
self, *args: Any, formdata: FormdataType = None, **kwargs: Any
self,
*args: Any,
formdata: FormdataType = None,
is_multipart: bool = False,
**kwargs: Any,
) -> None:
if formdata and kwargs.get("method") is None:
kwargs["method"] = "POST"
Expand All @@ -46,7 +50,12 @@ def __init__(
form_query_str = _urlencode(items, self.encoding)
if self.method == "POST":
self.headers.setdefault(
b"Content-Type", b"application/x-www-form-urlencoded"
b"Content-Type",
(
b"application/x-www-form-urlencoded"
if not is_multipart
else b"multipart/form-data"
),
)
self._set_body(form_query_str)
else:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,37 @@ def test_multi_key_values(self):
r3.body, b"colours=red&colours=blue&colours=green&price=%C2%A3+100"
)

def test_multipart_formdata(self):
data = {"foo": "bar"}
r1 = self.request_class(
"http://www.example.com", formdata=data, is_multipart=True
)
self.assertEqual(r1.headers[b"Content-Type"], b"multipart/form-data")

def test_from_response_multipart_formdata(self):
response = _buildresponse(
b"""<form action="post.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="foo" value="bar">
<input type="hidden" name="foo" value="baz">
<input type="hidden" name="bar" value="baz">
</form>""",
url="http://www.example.com/this/list.html",
)
req = self.request_class.from_response(
response,
formdata={"fullname": ["john", "doe"], "age": "30"},
is_multipart=True,
)

self.assertEqual(req.method, "POST")
self.assertEqual(req.headers[b"Content-type"], b"multipart/form-data")
self.assertEqual(req.url, "http://www.example.com/this/post.php")
fs = _qs(req)
self.assertEqual(set(fs[b"foo"]), {b"bar", b"baz"})
self.assertEqual(set(fs[b"fullname"]), {b"john", b"doe"})
self.assertEqual(fs[b"bar"], [b"baz"])
self.assertEqual(fs[b"age"], [b"30"])

def test_from_response_post(self):
response = _buildresponse(
b"""<form action="post.php" method="POST">
Expand Down