Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Fix JSONRequest naming (#3982)

Marc Hernándezace2df3d140a
diff --git a/docs/news.rst b/docs/news.rst
index 5915ba742..aac750601 100644
--- a/docs/news.rst
+++ b/docs/news.rst
@@ -80,8 +80,8 @@ New features
provides a cleaner way to pass keyword arguments to callback methods
(:issue:`1138`, :issue:`3563`)
-* A new :class:`~scrapy.http.JSONRequest` class offers a more convenient way
- to build JSON requests (:issue:`3504`, :issue:`3505`)
+* A new :class:`JSONRequest <scrapy.http.JsonRequest>` class offers a more
+ convenient way to build JSON requests (:issue:`3504`, :issue:`3505`)
* A ``process_request`` callback passed to the :class:`~scrapy.spiders.Rule`
constructor now receives the :class:`~scrapy.http.Response` object that
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst
index 9a5c65b0d..ad1b9af10 100644
--- a/docs/topics/request-response.rst
+++ b/docs/topics/request-response.rst
@@ -535,19 +535,19 @@ method for this job. Here's an example spider which uses it::
# continue scraping with authenticated session...
-JSONRequest
+JsonRequest
-----------
-The JSONRequest class extends the base :class:`Request` class with functionality for
+The JsonRequest class extends the base :class:`Request` class with functionality for
dealing with JSON requests.
-.. class:: JSONRequest(url, [... data, dumps_kwargs])
+.. class:: JsonRequest(url, [... data, dumps_kwargs])
- The :class:`JSONRequest` class adds two new argument to the constructor. The
+ The :class:`JsonRequest` class adds two new argument to the constructor. The
remaining arguments are the same as for the :class:`Request` class and are
not documented here.
- Using the :class:`JSONRequest` will set the ``Content-Type`` header to ``application/json``
+ Using the :class:`JsonRequest` will set the ``Content-Type`` header to ``application/json``
and ``Accept`` header to ``application/json, text/javascript, */*; q=0.01``
:param data: is any JSON serializable object that needs to be JSON encoded and assigned to body.
@@ -562,7 +562,7 @@ dealing with JSON requests.
.. _json.dumps: https://docs.python.org/3/library/json.html#json.dumps
-JSONRequest usage example
+JsonRequest usage example
-------------------------
Sending a JSON POST request with a JSON payload::
@@ -571,7 +571,7 @@ Sending a JSON POST request with a JSON payload::
'name1': 'value1',
'name2': 'value2',
}
- yield JSONRequest(url='http://www.example.com/post/action', data=data)
+ yield JsonRequest(url='http://www.example.com/post/action', data=data)
Response objects
diff --git a/scrapy/http/__init__.py b/scrapy/http/__init__.py
index 4b2f7b33f..e6c58e1f1 100644
--- a/scrapy/http/__init__.py
+++ b/scrapy/http/__init__.py
@@ -10,7 +10,7 @@ from scrapy.http.headers import Headers
from scrapy.http.request import Request
from scrapy.http.request.form import FormRequest
from scrapy.http.request.rpc import XmlRpcRequest
-from scrapy.http.request.json_request import JSONRequest
+from scrapy.http.request.json_request import JsonRequest
from scrapy.http.response import Response
from scrapy.http.response.html import HtmlResponse
diff --git a/scrapy/http/request/json_request.py b/scrapy/http/request/json_request.py
index 8f7a61a6d..f08b25280 100644
--- a/scrapy/http/request/json_request.py
+++ b/scrapy/http/request/json_request.py
@@ -1,5 +1,5 @@
"""
-This module implements the JSONRequest class which is a more convenient class
+This module implements the JsonRequest class which is a more convenient class
(than Request) to generate JSON Requests.
See documentation in docs/topics/request-response.rst
@@ -10,9 +10,10 @@ import json
import warnings
from scrapy.http.request import Request
+from scrapy.utils.deprecate import create_deprecated_class
-class JSONRequest(Request):
+class JsonRequest(Request):
def __init__(self, *args, **kwargs):
dumps_kwargs = copy.deepcopy(kwargs.pop('dumps_kwargs', {}))
dumps_kwargs.setdefault('sort_keys', True)
@@ -31,7 +32,7 @@ class JSONRequest(Request):
if 'method' not in kwargs:
kwargs['method'] = 'POST'
- super(JSONRequest, self).__init__(*args, **kwargs)
+ super(JsonRequest, self).__init__(*args, **kwargs)
self.headers.setdefault('Content-Type', 'application/json')
self.headers.setdefault('Accept', 'application/json, text/javascript, */*; q=0.01')
@@ -46,8 +47,11 @@ class JSONRequest(Request):
elif not body_passed and data_passed:
kwargs['body'] = self._dumps(data)
- return super(JSONRequest, self).replace(*args, **kwargs)
+ return super(JsonRequest, self).replace(*args, **kwargs)
def _dumps(self, data):
"""Convert to JSON """
return json.dumps(data, **self._dumps_kwargs)
+
+
+JSONRequest = create_deprecated_class("JSONRequest", JsonRequest)
diff --git a/tests/test_http_request.py b/tests/test_http_request.py
index 60494d792..16d7a1cb8 100644
--- a/tests/test_http_request.py
+++ b/tests/test_http_request.py
@@ -11,7 +11,7 @@ from six.moves.urllib.parse import urlparse, parse_qs, unquote
if six.PY3:
from urllib.parse import unquote_to_bytes
-from scrapy.http import Request, FormRequest, XmlRpcRequest, JSONRequest, Headers, HtmlResponse
+from scrapy.http import Request, FormRequest, XmlRpcRequest, JsonRequest, Headers, HtmlResponse
from scrapy.utils.python import to_bytes, to_native_str
from tests import mock
@@ -1246,14 +1246,14 @@ class XmlRpcRequestTest(RequestTest):
self._test_request(params=(u'pas£',), encoding='latin1')
-class JSONRequestTest(RequestTest):
- request_class = JSONRequest
+class JsonRequestTest(RequestTest):
+ request_class = JsonRequest
default_method = 'GET'
default_headers = {b'Content-Type': [b'application/json'], b'Accept': [b'application/json, text/javascript, */*; q=0.01']}
def setUp(self):
warnings.simplefilter("always")
- super(JSONRequestTest, self).setUp()
+ super(JsonRequestTest, self).setUp()
def test_data(self):
r1 = self.request_class(url="http://www.example.com/")
@@ -1407,7 +1407,7 @@ class JSONRequestTest(RequestTest):
def tearDown(self):
warnings.resetwarnings()
- super(JSONRequestTest, self).tearDown()
+ super(JsonRequestTest, self).tearDown()
if __name__ == "__main__":